-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoroutine_test.go
143 lines (125 loc) · 5.05 KB
/
goroutine_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
//"log"
"reflect"
"strconv"
"sync"
"testing"
)
func getTestKvBackend(t *testing.T) KvBackend {
if _, ok := dockerContainerPorts["etcd_1-2379/tcp"]; ok == false {
t.Fatal("Docker Container port for etcd not found in dockerContainerPorts, did the container start?")
}
test_kv_backend, err := CreateKvBackend(map[string]string{
"backend": "etcd",
"host": dockerHost,
"port": strconv.Itoa(int(dockerContainerPorts["etcd_1-2379/tcp"])),
"prefix": "fs_test_registrations",
})
if err != nil {
t.Fatal(err)
}
return test_kv_backend
}
func TestWatchForRegistrationEvents(t *testing.T) {
test_esl_client := getTestEslClient(t)
test_kv_backend := getTestKvBackend(t)
test_advertise_ip := "192.168.99.100"
test_advertise_port := 5062
test_sip_user := "1002"
test_sip_pass := "1234"
test_sip_contact_port := uint(49203)
expected_result1 := map[string]string{
"[email protected]": "{\"host\":\"192.168.99.100\",\"port\":5062}",
}
// result 2 is an empty map
var test_wg sync.WaitGroup
go test_esl_client.Handle()
// This channel is triggered on each event execution.
event_channel := make(chan struct{})
defer close(event_channel)
// Start our watcher which will update the K/V store on changes.
test_wg.Add(1)
go watchForRegistrationEvents(test_esl_client, test_advertise_ip, test_advertise_port, test_kv_backend, &test_wg, 3, event_channel)
// Make sure that we are watching for events before proceeding.
//log.Printf("Wait for event: 1\n")
<-event_channel
//log.Printf("Done waiting for event: 1\n")
// Do a SIP register, so we have something to start with.
simulateSipRegister(dockerHost, uint(dockerContainerPorts["freeswitch_1-5060/udp"]), test_sip_user, test_sip_pass, test_sip_contact_port, t)
// Wait until an event is processed before proceeding.
//log.Printf("Wait for event: 2\n")
<-event_channel
//log.Printf("Done waiting for event: 2\n")
// Check it's in etcd.
result1, err := test_kv_backend.Read("", true)
if err != nil {
t.Fatal(err)
}
//log.Printf("TestWatchForRegistrationEvents() Read Error: %+v\n", err)
//log.Printf("TestWatchForRegistrationEvents() Read Result 1: %+v\n", result1)
if reflect.DeepEqual(*result1, expected_result1) != true {
t.Error("Expected", expected_result1, "got", result1)
}
// Cleanup the registration, before performing another sync.
simulateSipDeregister(dockerHost, uint(dockerContainerPorts["freeswitch_1-5060/udp"]), test_sip_user, test_sip_pass, test_sip_contact_port, t)
// Wait until an event is processed before proceeding.
//log.Printf("Wait for event: 3\n")
<-event_channel
//log.Printf("Done waiting for event: 3\n")
// Check it's gone from etcd.
result2, err := test_kv_backend.Read("", true)
if err != nil {
t.Fatal(err)
}
//log.Printf("TestWatchForRegistrationEvents() Read Error 2: %+v\n", err)
//log.Printf("TestWatchForRegistrationEvents() Read Result 2: %+v\n", result2)
if len(*result2) > 0 {
t.Errorf("Expected a zero length result from K/V backend, got %d results: %+v\n", len(*result2), *result2)
}
}
func TestSyncRegistrations(t *testing.T) {
test_esl_client := getTestEslClient(t)
test_kv_backend := getTestKvBackend(t)
checkSipPortIsAvailable(t)
test_sofia_profiles := []string{"internal"}
test_advertise_ip := "192.168.99.100"
test_advertise_port := 5061
test_sip_user := "1001"
test_sip_pass := "1234"
test_sip_contact_port := uint(49202)
expected_result1 := map[string]string{
"[email protected]": "{\"host\":\"192.168.99.100\",\"port\":5061}",
}
// result 2 is an empty map
// Do a SIP register, so we have something to start with.
simulateSipRegister(dockerHost, uint(dockerContainerPorts["freeswitch_1-5060/udp"]), test_sip_user, test_sip_pass, test_sip_contact_port, t)
var test_wg sync.WaitGroup
go test_esl_client.Handle()
// First sync, should perform an add to the K/V backend.
test_wg.Add(1)
syncRegistrations(test_esl_client, test_sofia_profiles, test_advertise_ip, test_advertise_port, 300, test_kv_backend, &test_wg, true)
result1, err := test_kv_backend.Read("", true)
if err != nil {
t.Fatal(err)
}
//log.Printf("TestSyncRegistrations() Read Error: %+v\n", err)
//log.Printf("TestSyncRegistrations() Read Result 1: %+v\n", result1)
if reflect.DeepEqual(*result1, expected_result1) != true {
t.Error("Expected", expected_result1, "got", result1)
}
// Cleanup the registration, before performing another sync.
simulateSipDeregister(dockerHost, uint(dockerContainerPorts["freeswitch_1-5060/udp"]), test_sip_user, test_sip_pass, test_sip_contact_port, t)
// Second sync, should perform a remove from the K/V backend.
test_wg.Add(1)
syncRegistrations(test_esl_client, test_sofia_profiles, test_advertise_ip, test_advertise_port, 300, test_kv_backend, &test_wg, true)
result2, err := test_kv_backend.Read("", true)
if err != nil {
t.Fatal(err)
}
//log.Printf("TestSyncRegistrations() Read Error 2: %+v\n", err)
//log.Printf("TestSyncRegistrations() Read Result 2: %+v\n", result2)
if len(*result2) > 0 {
t.Errorf("Expected a zero length result from K/V backend, got %d results: %+v\n", len(*result2), *result2)
}
}