Skip to content

Commit 13e0c07

Browse files
committed
chore: refactor test using signaling
1 parent 6edcfff commit 13e0c07

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

pkg/core/transport_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io/ioutil"
99
"net"
1010
"testing"
11-
"time"
1211

1312
"github.com/lucas-clemente/quic-go"
1413
"github.com/lucas-clemente/quic-go/congestion"
@@ -49,17 +48,18 @@ const (
4948
func TestE2E(t *testing.T) {
5049
// Server and Client share the same obfuscator
5150
obfuscator := obfs.NewXPlusObfuscator([]byte(obfs_str))
51+
signal := make(chan struct{})
5252

53-
go runServer(obfuscator)
53+
go runServer(obfuscator, signal)
5454

55-
err := runClient(obfuscator)
55+
err := runClient(obfuscator, signal)
5656
if err != nil {
5757
t.Fail()
5858
}
5959
}
6060

6161
// Simulate a server
62-
func runServer(obfuscator *obfs.XPlusObfuscator) error {
62+
func runServer(obfuscator *obfs.XPlusObfuscator, signal chan struct{}) error {
6363
// Load TLS server config
6464
cer, err := tls.LoadX509KeyPair(certFile, keyFile)
6565
if err != nil {
@@ -124,7 +124,9 @@ func runServer(obfuscator *obfs.XPlusObfuscator) error {
124124
fmt.Println("Failed to initialize server")
125125
}
126126

127+
serverBuffer := make([]byte, len(test_request))
127128
fmt.Println("Server up and running")
129+
signal <- struct{}{}
128130

129131
serverConn, err := l.Accept()
130132
defer serverConn.Close()
@@ -133,8 +135,6 @@ func runServer(obfuscator *obfs.XPlusObfuscator) error {
133135
return err
134136
}
135137

136-
time.Sleep(time.Second * 2)
137-
serverBuffer := make([]byte, len(test_request))
138138
fmt.Println("Server starts reading from connection")
139139
_, err = serverConn.Read(serverBuffer)
140140

@@ -145,6 +145,7 @@ func runServer(obfuscator *obfs.XPlusObfuscator) error {
145145
s := string(serverBuffer)
146146
if s == test_request {
147147
fmt.Println("Server received the expected data from the client")
148+
signal <- struct{}{}
148149
_, err = serverConn.Write([]byte(test_response))
149150
fmt.Println("Server sent the response to the client")
150151
return err
@@ -154,7 +155,7 @@ func runServer(obfuscator *obfs.XPlusObfuscator) error {
154155
}
155156

156157
// Simulate a client
157-
func runClient(obfuscator *obfs.XPlusObfuscator) error {
158+
func runClient(obfuscator *obfs.XPlusObfuscator, signal chan struct{}) error {
158159
// Load TLS client config
159160
var clientTlsConfig = &tls.Config{
160161
InsecureSkipVerify: false,
@@ -188,6 +189,7 @@ func runClient(obfuscator *obfs.XPlusObfuscator) error {
188189
EnableDatagrams: true,
189190
}
190191

192+
<-signal
191193
client, err := NewClient(server_addr, protocol, []byte(auth_str), clientTlsConfig, quicConfig,
192194
transport.DefaultClientTransport, client_up_mbps, client_down_mbps,
193195
congestionFactory, obfuscator)
@@ -207,14 +209,13 @@ func runClient(obfuscator *obfs.XPlusObfuscator) error {
207209
}
208210

209211
// write data from clientConn for server to read
210-
time.Sleep(time.Second * 2)
211212
_, err = clientConn.Write([]byte(test_request))
212213
if err != nil {
213214
return err
214215
}
215216
fmt.Println("Client sent the data to the server")
216217

217-
time.Sleep(time.Second * 5)
218+
<-signal
218219
clientBuffer := make([]byte, len(test_response))
219220
fmt.Println("Client starts reading from connection")
220221
_, err = clientConn.Read(clientBuffer)

0 commit comments

Comments
 (0)