-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontext_test.go
127 lines (107 loc) · 2.26 KB
/
context_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package grid
import (
"context"
"net"
"sync"
"testing"
"time"
"github.com/lytics/grid/v3/testetcd"
)
type contextActor struct {
mu sync.RWMutex
started chan bool
ctx context.Context
}
func (a *contextActor) Act(c context.Context) {
a.mu.Lock()
a.ctx = c
a.mu.Unlock()
a.started <- true
}
func (a *contextActor) Context() context.Context {
a.mu.RLock()
defer a.mu.RUnlock()
return a.ctx
}
func TestContextError(t *testing.T) {
// Create a context that is not valid to use
// with the grid context methods. The context
// is not valid because it does not contain
// all the needed keys and values.
c := context.Background()
id, err := ContextActorID(c)
if err == nil {
t.Fatal("expected error")
}
if id != "" {
t.Fatal("expected zero value")
}
name, err := ContextActorName(c)
if err == nil {
t.Fatal("expected error")
}
if name != "" {
t.Fatal("expected zero value")
}
namespace, err := ContextActorNamespace(c)
if err == nil {
t.Fatal("expected error")
}
if namespace != "" {
t.Fatal("expected zero value")
}
}
func TestValidContext(t *testing.T) {
const timeout = 2 * time.Second
etcd := testetcd.StartAndConnect(t)
a := &contextActor{started: make(chan bool)}
server, err := NewServer(etcd, ServerCfg{Namespace: newNamespace(t)})
if err != nil {
t.Fatal(err)
}
server.RegisterDef("leader", func(_ []byte) (Actor, error) { return a, nil })
// Create the listener on a random port.
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
// Start the server in the background.
done := make(chan error, 1)
go func() {
err = server.Serve(lis)
if err != nil {
done <- err
}
}()
time.Sleep(timeout)
for {
select {
case <-time.After(10 * time.Second):
t.Fatal("timeout")
case <-a.started:
server.Stop()
id, err := ContextActorID(a.Context())
if err != nil {
t.Fatal(err)
}
if id == "" {
t.Fatal("expected non-zero value")
}
name, err := ContextActorName(a.Context())
if err != nil {
t.Fatal(err)
}
if name == "" {
t.Fatal("expected non-zero value")
}
namespace, err := ContextActorNamespace(a.Context())
if err != nil {
t.Fatal(err)
}
if namespace == "" {
t.Fatal("expected non-zero value")
}
return
}
}
}