/
wait.go
76 lines (70 loc) · 1.96 KB
/
wait.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
package router
import (
"time"
eventpkg "github.com/serverless/event-gateway/event"
"github.com/serverless/event-gateway/function"
)
// WaitForFunction returns a chan that is closed when a function is created.
// Primarily for testing purposes.
func (router *Router) WaitForFunction(space string, id function.ID) <-chan struct{} {
updatedChan := make(chan struct{})
go func() {
for {
res := router.targetCache.Function(space, id)
if res != nil {
break
}
time.Sleep(50 * time.Millisecond)
}
close(updatedChan)
}()
return updatedChan
}
// WaitForAsyncSubscriber returns a chan that is closed when an event has a subscriber.
// Primarily for testing purposes.
func (router *Router) WaitForAsyncSubscriber(method, path string, eventType eventpkg.TypeName) <-chan struct{} {
updatedChan := make(chan struct{})
go func() {
for {
res := router.targetCache.AsyncSubscribers(method, path, eventType)
if len(res) > 0 {
break
}
time.Sleep(50 * time.Millisecond)
}
close(updatedChan)
}()
return updatedChan
}
// WaitForSyncSubscriber returns a chan that is closed when an a sync subscriber is created.
// Primarily for testing purposes.
func (router *Router) WaitForSyncSubscriber(method, path string, eventType eventpkg.TypeName) <-chan struct{} {
updatedChan := make(chan struct{})
go func() {
for {
subscriber := router.targetCache.SyncSubscriber(method, path, eventType)
if subscriber != nil {
break
}
time.Sleep(50 * time.Millisecond)
}
close(updatedChan)
}()
return updatedChan
}
// WaitForEventType returns a chan that is closed when a event type is created.
// Primarily for testing purposes.
func (router *Router) WaitForEventType(space string, name eventpkg.TypeName) <-chan struct{} {
updatedChan := make(chan struct{})
go func() {
for {
res := router.targetCache.EventType(space, name)
if res != nil {
break
}
time.Sleep(50 * time.Millisecond)
}
close(updatedChan)
}()
return updatedChan
}