forked from benalexau/ibconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
56 lines (48 loc) · 1.34 KB
/
testing.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
package server
import (
"github.com/benalexau/ibconnect/core"
"github.com/benalexau/ibconnect/gateway"
"net/http"
"testing"
"time"
)
// NewTestHandler returns the same handler chain as the server will usually use.
// Be sure to defer Context.Close().
func NewTestHandler(t *testing.T) (*core.Context, http.Handler) {
c, err := core.NewConfig()
if err != nil {
t.Fatal(err)
}
ctx, err := core.NewContext(c)
if err != nil {
t.Fatal(err)
}
return ctx, Handler(c.ErrInfo, ctx.DB, ctx.N)
}
// WaitForFeed blocks the goroutine until the FeedFactory has sent a Done event.
// This is useful for ensuring the database has some content.
func WaitForFeed(t *testing.T, ctx *core.Context, ff *gateway.FeedFactory, timeout time.Duration) {
// Prepare feed context
fct := gateway.NewTestFeedContext(t)
defer fct.Close()
// Prepare channel to monitor when feed has completed its work
notifications := make(chan *core.Notification)
fct.FC.N.Subscribe(notifications)
defer fct.FC.N.Unsubscribe(notifications)
// Run feed
feed := (*ff).NewFeed(fct.FC)
defer (*feed).Close()
// Wait
for {
select {
case err := <-fct.FC.Errors:
t.Fatalf("error while waiting: %v", err)
case event := <-notifications:
if event.Type == (*ff).Done() {
return
}
case <-time.After(timeout):
t.Fatalf("timeout waiting for %v", (*ff).Done())
}
}
}