-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopserve_test.go
106 lines (104 loc) · 1.88 KB
/
topserve_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
package topserve
import (
"fmt"
"testing"
"time"
)
func TestPublishValues(t *testing.T) {
var server Server
server, err := server.New(":7777")
if err != nil {
t.Log(err)
t.Fail()
}
go func() {
for {
c, err := server.AcceptConnection()
if err != nil {
fmt.Println(err)
} else {
go server.HandleConnection(c)
}
}
}()
var client1 Client
client1, err = client1.New(":7777")
if err != nil {
t.Log(err)
t.Fail()
}
go client1.HandleIncomings()
var client2 Client
client2, err = client2.New(":7777")
if err != nil {
t.Log(err)
t.Fail()
}
go client2.HandleIncomings()
err = client1.RegisterPublisher("test:test")
if err != nil {
t.Log(err)
t.Fail()
}
err = client2.RegisterSubscriber("test:test")
if err != nil {
t.Log(err)
t.Fail()
}
var num int = 0
value := make(map[string]interface{})
value["value1"] = 46
value["value2"] = "string"
err = client2.Subscribe("test:test", func(v interface{}, unixTime int64) {
num++
m := v.(map[string]interface{})
fmt.Println(v)
if m["value1"].(float64) != 46 {
t.Log("value did not recive from client 1")
t.Fail()
}
if m["value2"] != "string" {
t.Log("value did not recive from client 1")
t.Fail()
}
})
if err != nil {
t.Log(err)
t.Fail()
}
err = client1.PublishServer("test:test", value)
if err != nil {
t.Log(err)
t.Fail()
}
time.Sleep(time.Millisecond * 10)
if num == 0 {
t.Log("subscriber didnt run")
t.Fail()
}
err = client2.DeRegisterSubscriber("test:test")
if err != nil {
t.Log(err)
t.Fail()
}
err = client1.PublishServer("test:test", value)
if err != nil {
t.Log(err)
t.Fail()
}
if num == 2 {
t.Log("subscriber runs after delete")
t.Fail()
}
err = client1.DeRegisterPublisher("test:test")
if err != nil {
t.Log(err)
t.Fail()
}
err = client2.RegisterSubscriber("test:test")
if err == nil {
t.Fail()
}
client1.End()
client2.End()
}