-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
210 lines (178 loc) · 5.64 KB
/
main_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"fmt"
"github.com/HarrisonWAffel/terminal-chat/internal/client"
"github.com/HarrisonWAffel/terminal-chat/internal/server"
"github.com/pion/webrtc/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log"
"os"
"sync"
"testing"
"time"
)
var httpCtx = &server.AppCtx{
Log: *log.New(os.Stdout, "", 0),
ServerURL: "http://localhost:9999",
ServerCtx: &server.ServerCtx{
Port: ":9999",
},
}
var GrpcCtx = &server.AppCtx{
GRPCEnabled: true,
Log: *log.New(os.Stdout, "", 0),
ServerURL: "http://localhost:9998",
ServerCtx: &server.ServerCtx{
Port: ":9998",
},
}
func init() {
go server.StartHTTPServer(httpCtx)
go server.StartGRPCServer(GrpcCtx)
conn, err := grpc.Dial(GrpcCtx.ServerCtx.Port, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
GrpcCtx.DiscoveryClient = server.NewDiscoveryClient(conn)
}
func TestHTTP(t *testing.T) {
h, recv := NewHTTPTestHostClient(httpCtx)
r := NewHTTPTestReceiverClient(httpCtx)
go h.HostNewConversation(httpCtx, client.ConnectionConfig{CustomToken: "testing"})
time.Sleep(10 * time.Second) // just needs to be longer than the offer clients ICE candidate gathering
go r.ConnectToConversationToken(httpCtx, "testing")
ReceiveMessageOrFailTest(t, recv)
}
func TestGRPC(t *testing.T) {
h, recv := NewGRPCTestHostClient(GrpcCtx)
r := NewGRPCTestReceiverClient(GrpcCtx)
go h.HostNewConversation(GrpcCtx, client.ConnectionConfig{CustomToken: "testing"})
time.Sleep(10 * time.Second) // just needs to be longer than the offer clients ICE candidate gathering
go r.ConnectToConversationToken(GrpcCtx, "testing")
ReceiveMessageOrFailTest(t, recv)
}
func TestMultipleHTTP(t *testing.T) {
wg := &sync.WaitGroup{}
for i := 0; i < 25; i++ {
wg.Add(1)
go func(i int, wg *sync.WaitGroup) {
h1, recv1 := NewHTTPTestHostClient(httpCtx)
r := NewHTTPTestReceiverClient(httpCtx)
go h1.HostNewConversation(httpCtx, client.ConnectionConfig{CustomToken: fmt.Sprintf("test%d", i)})
time.Sleep(10 * time.Second)
go r.ConnectToConversationToken(httpCtx, fmt.Sprintf("test%d", i))
ReceiveMessageOrFailTest(t, recv1)
wg.Done()
}(i, wg)
}
wg.Wait()
}
func TestMutlipleGRPC(t *testing.T) {
h1, recv1 := NewGRPCTestHostClient(GrpcCtx)
r := NewGRPCTestReceiverClient(GrpcCtx)
h2, recv2 := NewGRPCTestHostClient(GrpcCtx)
r2 := NewGRPCTestReceiverClient(GrpcCtx)
go h1.HostNewConversation(GrpcCtx, client.ConnectionConfig{CustomToken: "test1"})
go h2.HostNewConversation(GrpcCtx, client.ConnectionConfig{CustomToken: "test2"})
time.Sleep(10 * time.Second)
go r.ConnectToConversationToken(GrpcCtx, "test1")
go r2.ConnectToConversationToken(GrpcCtx, "test2")
ReceiveMessageOrFailTest(t, recv1)
ReceiveMessageOrFailTest(t, recv2)
}
func ReceiveMessageOrFailTest(t *testing.T, channel chan string) {
L:
for {
select {
case <-channel:
break L
case <-time.After(time.Second * 25):
t.Log("failed to receive message after 25 seconds")
t.FailNow()
}
}
}
func NewHTTPTestReceiverClient(appCtx *server.AppCtx) client.ReceivingClient {
return &client.HTTPReceiver{Client: NewTestReceiverClient(appCtx)}
}
func NewHTTPTestHostClient(appCtx *server.AppCtx) (client.HostClient, chan string) {
c, recv := NewTestOfferClient(appCtx)
return &client.HTTPHost{Client: c}, recv
}
func NewGRPCTestReceiverClient(appCtx *server.AppCtx) client.ReceivingClient {
return &client.GRPCReceiver{Client: NewTestReceiverClient(appCtx)}
}
func NewGRPCTestHostClient(appCtx *server.AppCtx) (client.HostClient, chan string) {
c, recv := NewTestOfferClient(appCtx)
return &client.GRPCHost{Client: c}, recv
}
func NewTestReceiverClient(appCtx *server.AppCtx) *client.Client {
pc, err := webrtc.NewPeerConnection(client.Config)
if err != nil {
panic(err)
}
c := &client.Client{
AppCtx: appCtx,
PeerConnection: pc,
ICECandidateChan: make(chan webrtc.SessionDescription, 1),
}
c.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
if s == webrtc.PeerConnectionStateFailed {
fmt.Println("Peer Connection has gone to failed, exiting")
os.Exit(0)
}
})
c.OnDataChannel(func(d *webrtc.DataChannel) {
fmt.Printf("DataChannel %s-%d open\n\n", d.Label(), d.ID())
d.OnOpen(func() {
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", d.Label(), d.ID())
for range time.NewTicker(5 * time.Second).C {
message := "hello world"
fmt.Printf("Sending '%s'\n", message)
// Send the message as text
sendErr := d.SendText(message)
if sendErr != nil {
panic(sendErr)
}
}
})
d.OnMessage(func(msg webrtc.DataChannelMessage) {
fmt.Println(string(msg.Data))
})
})
return c
}
func NewTestOfferClient(appCtx *server.AppCtx) (*client.Client, chan string) {
pc, err := webrtc.NewPeerConnection(client.Config)
if err != nil {
panic(err)
}
c := &client.Client{
AppCtx: appCtx,
PeerConnection: pc,
ICECandidateChan: make(chan webrtc.SessionDescription, 1),
}
d, err := c.CreateDataChannel("conversation", nil)
if err != nil {
panic(err)
}
d.OnClose(func() {
fmt.Println("data channel has closed")
})
c.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
if s == webrtc.PeerConnectionStateFailed {
fmt.Println("Peer Connection has gone to failed, exiting")
os.Exit(0)
}
})
d.OnOpen(func() {})
recv := make(chan string)
d.OnMessage(func(msg webrtc.DataChannelMessage) {
fmt.Println(string(msg.Data))
recv <- string(msg.Data)
})
return c, recv
}