-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_test.go
71 lines (56 loc) · 1.17 KB
/
queue_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
package main
import (
"fmt"
"sync"
"testing"
"gitgud.io/softashell/comfy-translator/translator"
)
func TestQueue(t *testing.T) {
q := NewQueue()
req := translator.Request{
Text: "test",
}
if len(q.items) != 0 {
t.Error("Queue not empty?")
}
ch, wait := q.Join(req)
if ch != nil {
t.Error("Returned unexpected channel")
}
if wait == true {
t.Error("We shouldn't wait here")
}
if len(q.items) != 1 {
t.Error("Queue does not contain one item")
}
wg := sync.WaitGroup{}
joinWait(t, q, req, &wg, "test")
joinWait(t, q, req, &wg, "test")
joinWait(t, q, req, &wg, "test")
if q.items[0].count != 3 {
t.Error("Does not have enough waiting jobs")
}
q.Push(req, "test")
if len(q.items) != 0 {
t.Error("Queue not empty")
}
wg.Wait()
}
func joinWait(t *testing.T, q *Queue, req translator.Request, wg *sync.WaitGroup, expecting string) {
ch, wait := q.Join(req)
if wait != true {
t.Error("We should wait here")
}
if ch == nil {
t.Error("Didn't return channel")
}
go func(chan string) {
wg.Add(1)
defer wg.Done()
out := <-ch
fmt.Println("got", out)
if out != expecting {
t.Error("Unexpected output for waiting function")
}
}(ch)
}