-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemux_test.go
More file actions
102 lines (82 loc) · 1.83 KB
/
Copy pathdemux_test.go
File metadata and controls
102 lines (82 loc) · 1.83 KB
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
package goat_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/neat-no/goat"
"github.com/neat-no/goat/gen/goatorepo"
)
func TestDemuxSource(t *testing.T) {
t.Run("multiple clients, single request", func(t *testing.T) {
is := require.New(t)
r := make(chan *goat.Rpc)
w := make(chan *goat.Rpc)
rw := goat.NewGoatOverChannel(r, w)
conn := make(chan goat.RpcReadWriter)
demux := goat.NewDemux(
context.Background(),
rw,
func(rpc *goat.Rpc) string {
return rpc.Header.Source
},
func(rrw goat.RpcReadWriter) {
conn <- rrw
},
)
go demux.Run()
t.Cleanup(demux.Stop)
for i := 0; i < 10; i++ {
client := fmt.Sprintf("client%d", i)
rpc := &goatorepo.Rpc{
Id: 1,
Header: &goatorepo.RequestHeader{
Source: client,
},
}
r <- rpc
c := <-conn
rpc, err := c.Read(context.Background())
is.NoError(err)
is.Equal(rpc.GetId(), uint64(1))
is.Equal(rpc.GetHeader().GetSource(), client)
}
})
t.Run("single client, multiple requests", func(t *testing.T) {
is := require.New(t)
r := make(chan *goat.Rpc)
w := make(chan *goat.Rpc)
rw := goat.NewGoatOverChannel(r, w)
conn := make(chan goat.RpcReadWriter)
demux := goat.NewDemux(
context.Background(),
rw,
func(rpc *goat.Rpc) string {
return rpc.Header.Source
},
func(rrw goat.RpcReadWriter) {
conn <- rrw
},
)
go demux.Run()
t.Cleanup(demux.Stop)
client := "client"
var c goat.RpcReadWriter
for i := 1; i < 10; i++ {
rpc := &goatorepo.Rpc{
Id: uint64(i),
Header: &goatorepo.RequestHeader{
Source: client,
},
}
r <- rpc
if i == 1 {
c = <-conn
}
rpc, err := c.Read(context.Background())
is.NoError(err)
is.Equal(rpc.GetId(), uint64(i))
is.Equal(rpc.GetHeader().GetSource(), client)
}
})
}