This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
/
server_test.go
92 lines (79 loc) · 1.7 KB
/
server_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
package qrpc
import (
"fmt"
"strconv"
"sync"
"testing"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"github.com/kuba--/qrpc/api"
)
const (
topic = "qrpc-test"
)
var (
config = &Config{
DataBasePath: "/tmp/qrpc-test",
MaxCacheSize: 1024,
Port: 9090,
ClusterRequestTimeout: time.Second,
ClusterWatchInterval: time.Second,
}
)
func TestServer(t *testing.T) {
s := NewServer(config)
errchan := s.Start()
defer s.Stop()
select {
case err := <-errchan:
t.Error(err)
default:
testServerSend(t)
testServerReceive(t)
}
}
func testServerSend(t *testing.T) {
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func(cnt int) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), config.ClusterRequestTimeout)
msg := strconv.Itoa(cnt)
_, err := api.Request(ctx, ":"+strconv.Itoa(config.Port), &api.SendRequest{topic, []byte(msg)})
if err != nil {
cancel()
t.Error(err)
}
}(i)
}
wg.Wait()
}
func testServerReceive(t *testing.T) {
wg := &sync.WaitGroup{}
lookup := make(map[string]string)
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), config.ClusterRequestTimeout)
if r, err := api.Request(ctx, ":"+strconv.Itoa(config.Port), &api.ReceiveRequest{topic}); err != nil {
if grpc.Code(err) == codes.NotFound {
return
}
cancel()
t.Error(err)
} else {
fmt.Println(r)
resp := r.(*api.ReceiveResponse)
if v, b := lookup[resp.Key]; b {
t.Errorf("Msg: %v already exists: %v\n", resp, v)
}
lookup[resp.Key] = string(resp.Msg)
}
}()
}
wg.Wait()
}