MCPcopy Index your code
hub / github.com/DOSNetwork/core / TestServer

Function TestServer

p2p/server_test.go:25–83  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

23}
24
25func TestServer(t *testing.T) {
26 listener := []byte("9")
27 os.Setenv("PUBLICIP", "127.0.0.1")
28 log.Init(listener[:])
29
30 pListener, _ := CreateP2PNetwork(listener, "9905", NoDiscover)
31 pListener.Listen()
32
33 go receiveEvent(pListener, t)
34
35 var wgForPeer sync.WaitGroup
36 wgForPeer.Add(1)
37 for c := 9904; c < 9905; c++ {
38 go func(c int) {
39 defer wgForPeer.Done()
40 id := []byte(strconv.Itoa(c))
41 p, _ := CreateP2PNetwork(id, strconv.Itoa(c), NoDiscover)
42 p.Listen()
43
44 var count uint64
45 var wgForMsg sync.WaitGroup
46 var connected []byte
47 var err error
48 wgForMsg.Add(10)
49 for count = 0; count < 10; count++ {
50 go func(count uint64) {
51 defer wgForMsg.Done()
52 p.SetPort("9905")
53 connected, err = p.ConnectTo("127.0.0.1", nil)
54 if err != nil {
55 t.Errorf("ConnectTo ,Error %s", err)
56 }
57 cmd := &Ping{Count: count}
58 pb := proto.Message(cmd)
59 reply, _ := p.Request(connected, pb)
60 pong, _ := reply.Msg.Message.(*Pong)
61 if pong.Count-count != 10 {
62 t.Errorf("TestRequest ,Expected %d Actual %d", count+10, pong.Count)
63 }
64 p.DisConnectTo(connected)
65 }(count)
66 }
67 wgForMsg.Wait()
68 }(c)
69 }
70 wgForPeer.Wait()
71 retryLimit := 5
72 for {
73 prNum, pcNum := pListener.numOfClient()
74 if prNum == 0 && pcNum == 0 {
75 break
76 }
77 retryLimit--
78 if retryLimit == 0 {
79 t.Errorf("TestServer ,Expected %d Actual %d", 0, prNum)
80 }
81 time.Sleep(1 * time.Second)
82 }

Callers

nothing calls this directly

Calls 9

CreateP2PNetworkFunction · 0.85
receiveEventFunction · 0.85
ListenMethod · 0.65
SetPortMethod · 0.65
RequestMethod · 0.65
DisConnectToMethod · 0.65
numOfClientMethod · 0.65
InitMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected