1
1
package wsproxy_test
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/json"
6
7
"fmt"
@@ -497,8 +498,8 @@ func TestDERPMesh(t *testing.T) {
497
498
proxyURL ,err := url .Parse ("https://proxy.test.coder.com" )
498
499
require .NoError (t ,err )
499
500
500
- // Create6 proxy replicas.
501
- const count = 6
501
+ // Create3 proxy replicas.
502
+ const count = 3
502
503
var (
503
504
sessionToken = ""
504
505
proxies = [count ]coderdenttest.WorkspaceProxy {}
@@ -838,27 +839,33 @@ func TestWorkspaceProxyDERPMeshProbe(t *testing.T) {
838
839
require .NoError (t ,err )
839
840
840
841
// Create 1 real proxy replica.
841
- replicaPingErr := make (chan string ,4 )
842
+ replicaPingRes := make (chan replicaPingCallback ,4 )
842
843
proxy := coderdenttest .NewWorkspaceProxyReplica (t ,api ,client ,& coderdenttest.ProxyOptions {
843
844
Name :"proxy-2" ,
844
845
ProxyURL :proxyURL ,
845
- ReplicaPingCallback :func (_ []codersdk.Replica ,err string ) {
846
- replicaPingErr <- err
846
+ ReplicaPingCallback :func (replicas []codersdk.Replica ,err string ) {
847
+ t .Logf ("got wsproxy ping callback: replica count: %v, ping error: %s" ,len (replicas ),err )
848
+ replicaPingRes <- replicaPingCallback {
849
+ replicas :replicas ,
850
+ err :err ,
851
+ }
847
852
},
848
853
})
849
854
855
+ // Create a second proxy replica that isn't working.
850
856
ctx := testutil .Context (t ,testutil .WaitLong )
851
857
otherReplicaID := registerBrokenProxy (ctx ,t ,api .AccessURL ,proxyURL .String (),proxy .Options .ProxySessionToken )
852
858
853
- // Force the proxy to re-register immediately.
854
- err = proxy .RegisterNow ()
855
- require .NoError (t ,err ,"failed to force proxy to re-register" )
856
-
857
- // Wait for the ping to fail.
859
+ // Force the proxy to re-register and wait for the ping to fail.
858
860
for {
859
- replicaErr := testutil .TryReceive (ctx ,t ,replicaPingErr )
860
- t .Log ("replica ping error:" ,replicaErr )
861
- if replicaErr != "" {
861
+ err = proxy .RegisterNow ()
862
+ require .NoError (t ,err ,"failed to force proxy to re-register" )
863
+
864
+ pingRes := testutil .TryReceive (ctx ,t ,replicaPingRes )
865
+ // We want to ensure that we know about the other replica, and the
866
+ // ping failed.
867
+ if len (pingRes .replicas )== 1 && pingRes .err != "" {
868
+ t .Log ("got failed ping callback for other replica, continuing" )
862
869
break
863
870
}
864
871
}
@@ -884,17 +891,17 @@ func TestWorkspaceProxyDERPMeshProbe(t *testing.T) {
884
891
})
885
892
require .NoError (t ,err )
886
893
887
- // Force the proxy to re-register immediately.
888
- err = proxy .RegisterNow ()
889
- require .NoError (t ,err ,"failed to force proxy to re-register" )
890
-
891
- // Wait for the ping to be skipped.
894
+ // Force the proxy to re-register and wait for the ping to be skipped
895
+ // because there are no more siblings.
892
896
for {
893
- replicaErr := testutil .TryReceive (ctx ,t ,replicaPingErr )
894
- t .Log ("replica ping error:" ,replicaErr )
897
+ err = proxy .RegisterNow ()
898
+ require .NoError (t ,err ,"failed to force proxy to re-register" )
899
+
900
+ replicaErr := testutil .TryReceive (ctx ,t ,replicaPingRes )
895
901
// Should be empty because there are no more peers. This was where
896
902
// the regression was.
897
- if replicaErr == "" {
903
+ if len (replicaErr .replicas )== 0 && replicaErr .err == "" {
904
+ t .Log ("got empty ping callback with no sibling replicas, continuing" )
898
905
break
899
906
}
900
907
}
@@ -993,6 +1000,11 @@ func TestWorkspaceProxyWorkspaceApps(t *testing.T) {
993
1000
})
994
1001
}
995
1002
1003
+ type replicaPingCallback struct {
1004
+ replicas []codersdk.Replica
1005
+ err string
1006
+ }
1007
+
996
1008
func TestWorkspaceProxyWorkspaceApps_BlockDirect (t * testing.T ) {
997
1009
t .Parallel ()
998
1010
@@ -1118,7 +1130,7 @@ func createDERPClient(t *testing.T, ctx context.Context, name string, derpURL st
1118
1130
// received on dstCh.
1119
1131
//
1120
1132
// If the packet doesn't arrive within 500ms, it will try to send it again until
1121
- //testutil.WaitLong is reached .
1133
+ //the context expires .
1122
1134
//
1123
1135
//nolint:revive
1124
1136
func testDERPSend (t * testing.T ,ctx context.Context ,dstKey key.NodePublic ,dstCh <- chan derp.ReceivedPacket ,src * derphttp.Client ) {
@@ -1139,11 +1151,17 @@ func testDERPSend(t *testing.T, ctx context.Context, dstKey key.NodePublic, dstC
1139
1151
for {
1140
1152
select {
1141
1153
case pkt := <- dstCh :
1142
- require .Equal (t ,src .SelfPublicKey (),pkt .Source ,"packet came from wrong source" )
1143
- require .Equal (t ,msg ,pkt .Data ,"packet data is wrong" )
1154
+ if pkt .Source != src .SelfPublicKey () {
1155
+ t .Logf ("packet came from wrong source: %s" ,pkt .Source )
1156
+ continue
1157
+ }
1158
+ if ! bytes .Equal (pkt .Data ,msg ) {
1159
+ t .Logf ("packet data is wrong: %s" ,pkt .Data )
1160
+ continue
1161
+ }
1144
1162
return
1145
1163
case <- ctx .Done ():
1146
- t .Fatal ("timed out waiting for packet" )
1164
+ t .Fatal ("timed out waiting forvalid packet" )
1147
1165
return
1148
1166
case <- ticker .C :
1149
1167
}