8 func makeTestWorkList(ary []int) *list.List {
10 for _, n := range ary {
16 func expectChannelEmpty(t *testing.T, c <-chan interface{}) {
19 t.Fatalf("Received value (%v) from channel that we expected to be empty", item)
25 func expectChannelNotEmpty(t *testing.T, c <-chan interface{}) {
26 if item, ok := <-c; !ok {
27 t.Fatal("expected data on a closed channel")
28 } else if item == nil {
29 t.Fatal("expected data on an empty channel")
33 func expectChannelClosed(t *testing.T, c <-chan interface{}) {
36 t.Fatalf("Expected channel to be closed, but received %v instead", received)
40 func expectFromChannel(t *testing.T, c <-chan interface{}, expected []int) {
41 for i := range expected {
43 t.Logf("received %v", actual)
45 t.Fatalf("Expected %v but channel was closed after receiving the first %d elements correctly.", expected, i)
46 } else if actual.(int) != expected[i] {
47 t.Fatalf("Expected %v but received '%v' after receiving the first %d elements correctly.", expected[i], actual, i)
52 // Create a WorkQueue, generate a list for it, and instantiate a worker.
53 func TestWorkQueueReadWrite(t *testing.T) {
54 var input = []int{1, 1, 2, 3, 5, 8, 13, 21, 34}
57 b.ReplaceQueue(makeTestWorkList(input))
59 expectFromChannel(t, b.NextItem, input)
60 expectChannelEmpty(t, b.NextItem)
64 // Start a worker before the list has any input.
65 func TestWorkQueueEarlyRead(t *testing.T) {
66 var input = []int{1, 1, 2, 3, 5, 8, 13, 21, 34}
70 // First, demonstrate that nothing is available on the NextItem
72 expectChannelEmpty(t, b.NextItem)
74 // Start a reader in a goroutine. The reader will block until the
75 // block work list has been initialized.
77 done := make(chan int)
79 expectFromChannel(t, b.NextItem, input)
84 // Feed the blocklist a new worklist, and wait for the worker to
86 b.ReplaceQueue(makeTestWorkList(input))
89 expectChannelClosed(t, b.NextItem)
92 // Show that a reader may block when the manager's list is exhausted,
93 // and that the reader resumes automatically when new data is
95 func TestWorkQueueReaderBlocks(t *testing.T) {
97 inputBeforeBlock = []int{1, 2, 3, 4, 5}
98 inputAfterBlock = []int{6, 7, 8, 9, 10}
102 sendmore := make(chan int)
103 done := make(chan int)
105 expectFromChannel(t, b.NextItem, inputBeforeBlock)
107 // Confirm that the channel is empty, so a subsequent read
109 expectChannelEmpty(t, b.NextItem)
111 // Signal that we're ready for more input.
113 expectFromChannel(t, b.NextItem, inputAfterBlock)
118 // Write a slice of the first five elements and wait for the
119 // reader to signal that it's ready for us to send more input.
120 b.ReplaceQueue(makeTestWorkList(inputBeforeBlock))
123 b.ReplaceQueue(makeTestWorkList(inputAfterBlock))
125 // Wait for the reader to complete.
129 // Replace one active work list with another.
130 func TestWorkQueueReplaceQueue(t *testing.T) {
131 var firstInput = []int{1, 1, 2, 3, 5, 8, 13, 21, 34}
132 var replaceInput = []int{1, 4, 9, 16, 25, 36, 49, 64, 81}
135 b.ReplaceQueue(makeTestWorkList(firstInput))
137 // Read just the first five elements from the work list.
138 // Confirm that the channel is not empty.
139 expectFromChannel(t, b.NextItem, firstInput[0:5])
140 expectChannelNotEmpty(t, b.NextItem)
142 // Replace the work list and read five more elements.
143 // The old list should have been discarded and all new
144 // elements come from the new list.
145 b.ReplaceQueue(makeTestWorkList(replaceInput))
146 expectFromChannel(t, b.NextItem, replaceInput[0:5])