@ -14,6 +14,7 @@ import (
)
func Test_NewUploader ( t * testing . T ) {
ResetStats ( )
storageClient := & mockStorageClient { }
dataProvider := & mockDataProvider { }
interval := time . Second
@ -47,10 +48,10 @@ func Test_UploaderSingleUpload(t *testing.T) {
uploader := NewUploader ( sc , dp , 100 * time . Millisecond , UploadNoCompress )
ctx , cancel := context . WithCancel ( context . Background ( ) )
go uploader . Start ( ctx , nil )
done := uploader . Start ( ctx , nil )
wg . Wait ( )
cancel ( )
<- ctx. Done ( )
<- done
if exp , got := "my upload data" , string ( uploadedData ) ; exp != got {
t . Errorf ( "expected uploadedData to be %s, got %s" , exp , got )
@ -60,7 +61,6 @@ func Test_UploaderSingleUpload(t *testing.T) {
func Test_UploaderSingleUploadCompress ( t * testing . T ) {
ResetStats ( )
var uploadedData [ ] byte
var wg sync . WaitGroup
wg . Add ( 1 )
sc := & mockStorageClient {
@ -82,10 +82,10 @@ func Test_UploaderSingleUploadCompress(t *testing.T) {
uploader := NewUploader ( sc , dp , 100 * time . Millisecond , UploadCompress )
ctx , cancel := context . WithCancel ( context . Background ( ) )
go uploader . Start ( ctx , nil )
done := uploader . Start ( ctx , nil )
wg . Wait ( )
cancel ( )
<- ctx. Done ( )
<- done
if exp , got := "my upload data" , string ( uploadedData ) ; exp != got {
t . Errorf ( "expected uploadedData to be %s, got %s" , exp , got )
@ -94,7 +94,6 @@ func Test_UploaderSingleUploadCompress(t *testing.T) {
func Test_UploaderDoubleUpload ( t * testing . T ) {
ResetStats ( )
var uploadedData [ ] byte
var err error
@ -113,10 +112,10 @@ func Test_UploaderDoubleUpload(t *testing.T) {
uploader . disableSumCheck = true // Force upload of the same data
ctx , cancel := context . WithCancel ( context . Background ( ) )
go uploader . Start ( ctx , nil )
done := uploader . Start ( ctx , nil )
wg . Wait ( )
cancel ( )
<- ctx. Done ( )
<- done
if exp , got := "my upload data" , string ( uploadedData ) ; exp != got {
t . Errorf ( "expected uploadedData to be %s, got %s" , exp , got )
@ -125,7 +124,6 @@ func Test_UploaderDoubleUpload(t *testing.T) {
func Test_UploaderFailThenOK ( t * testing . T ) {
ResetStats ( )
var uploadedData [ ] byte
uploadCount := 0
var err error
@ -149,10 +147,10 @@ func Test_UploaderFailThenOK(t *testing.T) {
uploader := NewUploader ( sc , dp , 100 * time . Millisecond , UploadNoCompress )
ctx , cancel := context . WithCancel ( context . Background ( ) )
go uploader . Start ( ctx , nil )
done := uploader . Start ( ctx , nil )
wg . Wait ( )
cancel ( )
<- ctx. Done ( )
<- done
if exp , got := "my upload data" , string ( uploadedData ) ; exp != got {
t . Errorf ( "expected uploadedData to be %s, got %s" , exp , got )
@ -160,6 +158,7 @@ func Test_UploaderFailThenOK(t *testing.T) {
}
func Test_UploaderOKThenFail ( t * testing . T ) {
ResetStats ( )
var uploadedData [ ] byte
uploadCount := 0
var err error
@ -184,10 +183,10 @@ func Test_UploaderOKThenFail(t *testing.T) {
uploader . disableSumCheck = true // Disable because we want to upload twice.
ctx , cancel := context . WithCancel ( context . Background ( ) )
go uploader . Start ( ctx , nil )
done := uploader . Start ( ctx , nil )
wg . Wait ( )
cancel ( )
<- ctx. Done ( )
<- done
if exp , got := "my upload data" , string ( uploadedData ) ; exp != got {
t . Errorf ( "expected uploadedData to be %s, got %s" , exp , got )
@ -195,6 +194,7 @@ func Test_UploaderOKThenFail(t *testing.T) {
}
func Test_UploaderContextCancellation ( t * testing . T ) {
ResetStats ( )
var uploadCount int32
sc := & mockStorageClient {
@ -207,10 +207,9 @@ func Test_UploaderContextCancellation(t *testing.T) {
uploader := NewUploader ( sc , dp , time . Second , UploadNoCompress )
ctx , cancel := context . WithTimeout ( context . Background ( ) , 1 * time . Millisecond )
go uploader . Start ( ctx , nil )
<- ctx . Done ( )
done := uploader . Start ( ctx , nil )
cancel ( )
<- ctx. Done ( )
<- done
if exp , got := int32 ( 0 ) , atomic . LoadInt32 ( & uploadCount ) ; exp != got {
t . Errorf ( "expected uploadCount to be %d, got %d" , exp , got )
@ -219,25 +218,25 @@ func Test_UploaderContextCancellation(t *testing.T) {
func Test_UploaderEnabledFalse ( t * testing . T ) {
ResetStats ( )
sc := & mockStorageClient { }
dp := & mockDataProvider { data : "my upload data" }
uploader := NewUploader ( sc , dp , 100 * time . Millisecond , false )
ctx , cancel := context . WithCancel ( context . Background ( ) )
go uploader . Start ( ctx , func ( ) bool { return false } )
done := uploader . Start ( ctx , func ( ) bool { return false } )
time . Sleep ( time . Second )
defer cancel ( )
if exp , got := int64 ( 0 ) , stats . Get ( numUploadsOK ) . ( * expvar . Int ) ; exp != got . Value ( ) {
t . Errorf ( "expected numUploadsOK to be %d, got %d" , exp , got )
}
cancel ( )
<- done
}
func Test_UploaderEnabledTrue ( t * testing . T ) {
ResetStats ( )
var uploadedData [ ] byte
var err error
ResetStats ( )
var wg sync . WaitGroup
wg . Add ( 1 )
@ -252,16 +251,17 @@ func Test_UploaderEnabledTrue(t *testing.T) {
uploader := NewUploader ( sc , dp , 100 * time . Millisecond , UploadNoCompress )
ctx , cancel := context . WithCancel ( context . Background ( ) )
go uploader . Start ( ctx , func ( ) bool { return true } )
defer cancel ( )
done := uploader . Start ( ctx , func ( ) bool { return true } )
wg . Wait ( )
if exp , got := string ( uploadedData ) , "my upload data" ; exp != got {
t . Errorf ( "expected uploadedData to be %s, got %s" , exp , got )
}
cancel ( )
<- done
}
func Test_UploaderStats ( t * testing . T ) {
ResetStats ( )
sc := & mockStorageClient { }
dp := & mockDataProvider { data : "my upload data" }
interval := 100 * time . Millisecond