-
Notifications
You must be signed in to change notification settings - Fork 11
/
workers_test.go
77 lines (66 loc) · 1.46 KB
/
workers_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
package main
import (
"net/url"
"sync"
"testing"
"time"
)
func TestWorkerRegistration(t *testing.T) {
var (
w = newWorkerManager(time.Minute)
stop = make(chan struct{})
)
go w.start(stop)
defer close(stop)
// Identical URLs are only tracked once.
worker1 := url.URL{Host: "foo"}
w.register(&worker1)
w.register(&worker1)
assertEqual(t, w.length(), 1)
worker2 := url.URL{Host: "bar"}
w.register(&worker2)
assertEqual(t, w.length(), 2)
w.unregister(&worker1)
w.unregister(&worker2)
// It should be safe to unregister a non-existing worker.
w.unregister(&worker2)
assertEqual(t, w.length(), 0)
// Nothing should happen when attempting to unregister a non-existing
// worker.
w.unregister(&url.URL{Host: "does-not-exist"})
}
func TestForAll(t *testing.T) {
var (
w = newWorkerManager(time.Millisecond)
stop = make(chan struct{})
wg = sync.WaitGroup{}
mutex = sync.Mutex{}
total = 0
)
go w.start(stop)
defer close(stop)
w.register(&url.URL{Host: "foo"})
w.register(&url.URL{Host: "bar"})
assertEqual(t, w.length(), 2)
wg.Add(2)
w.forAll(
func(w *url.URL) {
mutex.Lock()
defer mutex.Unlock()
defer wg.Done()
total += 1
},
)
wg.Wait()
assertEqual(t, total, 2)
}
func TestIneffectiveForAll(t *testing.T) {
var (
w = newWorkerManager(time.Minute)
stop = make(chan struct{})
)
go w.start(stop)
defer close(stop)
// Make sure that forAll finishes for an empty worker set.
w.forAll(func(_ *url.URL) {})
}