-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker_test.go
85 lines (78 loc) · 2.21 KB
/
worker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cloudsync
import (
"bytes"
"context"
"errors"
"runtime"
"sync"
"testing"
"time"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/require"
)
func testShutdownUploadWorkers(t *testing.T) {
wg := sync.WaitGroup{}
initRoutines := runtime.NumGoroutine()
go ListenUploadErrors(Config{Scanner: ScannerConfig{LogErrors: false}})
require.Equal(t, initRoutines+1, runtime.NumGoroutine())
objectUploadJobQueueErr <- ErrFileUpload{
Key: "",
Parent: errors.New("testShutdownUploadWorkers: foo error"),
}
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
ShutdownUploadWorkers(ctx, &wg)
wg.Wait()
runtime.Gosched()
runtime.GC()
time.Sleep(time.Millisecond)
require.Equal(t, initRoutines, runtime.NumGoroutine())
}
func testListenUploadErrors(t *testing.T) {
initRoutines := runtime.NumGoroutine()
go ListenUploadErrors(Config{Scanner: ScannerConfig{LogErrors: true}})
objectUploadJobQueueErr <- ErrFileUpload{
Key: "",
Parent: errors.New("testListenUploadErrors: foo error"),
}
require.Equal(t, initRoutines+1, runtime.NumGoroutine())
close(objectUploadJobQueueErr)
objectUploadJobQueueErr = nil
runtime.Gosched()
runtime.GC()
time.Sleep(time.Millisecond)
require.Equal(t, initRoutines, runtime.NumGoroutine())
}
func testListenUpload(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(2)
initRoutines := runtime.NumGoroutine()
storage := &NoopBlobStorage{UploadErr: nil}
go ListenUploadErrors(Config{})
go ListenAndExecuteUploadJobs(context.TODO(), storage, &wg)
objectUploadJobQueue <- Object{
Key: "foo",
Data: bytes.NewReader([]byte("foo")),
CleanupFunc: func() error {
log.Debug().Msg("testListenUpload: cleaning up")
return nil
},
}
time.Sleep(time.Millisecond)
storage.UploadErr = errors.New("bar error")
objectUploadJobQueue <- Object{
Key: "bar",
Data: bytes.NewReader([]byte("bar")),
CleanupFunc: nil,
}
require.Equal(t, initRoutines+2, runtime.NumGoroutine())
wg.Wait()
close(objectUploadJobQueue)
objectUploadJobQueue = nil
close(objectUploadJobQueueErr)
objectUploadJobQueueErr = nil
runtime.Gosched()
runtime.GC()
time.Sleep(time.Millisecond)
require.Equal(t, initRoutines, runtime.NumGoroutine())
}