-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
61 lines (53 loc) · 1.15 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
package xcache
import (
"context"
"fmt"
"net/http"
_ "net/http/pprof"
"testing"
"time"
"xcache/protocol"
)
func startServer(port int) Server {
srv := NewServer(context.Background(), fmt.Sprintf(":%d", port), WithId(uint64(port)))
srv.ServeAsync(func(err error) {
log.Errorf("server error: %v", err)
})
return srv
}
func TestServer(t *testing.T) {
go func() {
err := http.ListenAndServe(":6060", nil)
assert(err)
}()
beginPort := 5001
node1 := &protocol.Node{
Addr: ":5000",
Id: 5000,
}
var servers []Server
for i := 0; i < 3; i++ {
srv := startServer(beginPort)
beginPort++
time.Sleep(time.Second)
err := srv.Join(node1)
if err != nil {
log.Fatal(err)
}
servers = append(servers, srv)
}
for {
time.Sleep(time.Second)
println("=======================================================================================================================")
for _, srv := range servers {
log.Debugf("server: %s nodes len = %d", srv.Myself().Addr, srv.NodeLen())
}
}
}
func TestExample(t *testing.T) {
srv := NewServer(context.Background(), ":5000", WithId(5000))
err := srv.Serve()
if err != nil {
t.Fatal(err)
}
}