@@ -2,18 +2,16 @@ package pss
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "math/rand"
8- "net/http"
97 "time"
108
119 "github.com/ethersphere/beekeeper/pkg/bee"
1210 "github.com/ethersphere/beekeeper/pkg/beekeeper"
1311 "github.com/ethersphere/beekeeper/pkg/logging"
1412 "github.com/ethersphere/beekeeper/pkg/orchestration"
1513 "github.com/ethersphere/beekeeper/pkg/random"
16- "github.com/gorilla/websocket "
14+ "github.com/ethersphere/beekeeper/pkg/wslistener "
1715)
1816
1917// Options represents check options
@@ -100,96 +98,60 @@ func pickAtRandom(r *rand.Rand, names []string, skip string) string {
10098 }
10199}
102100
103- var (
104- errDataMismatch = errors .New ("pss: data sent and received are not equal" )
105- errWebsocketConnection = errors .New ("pss: websocket connection terminated with an error" )
106- )
107-
108101var (
109102 testData = []byte ("Hello Swarm :)" )
110103 testTopic = "test"
111104)
112105
113106func (c * Check ) testPss (nodeAName , nodeBName string , clients map [string ]* bee.Client , o Options ) error {
114107 ctx , cancel := context .WithTimeout (context .Background (), o .RequestTimeout )
108+ defer cancel ()
115109
116110 nodeA := clients [nodeAName ]
117111 nodeB := clients [nodeBName ]
118112
119113 addrB , err := nodeB .Addresses (ctx )
120114 if err != nil {
121- cancel ()
122115 return err
123116 }
124117
125118 batchID , err := nodeA .GetOrCreateMutableBatch (ctx , o .PostageTTL , o .PostageDepth , o .PostageLabel )
126119 if err != nil {
127- cancel ()
128120 return fmt .Errorf ("node %s: batched id %w" , nodeAName , err )
129121 }
130122 c .logger .Infof ("node %s: batched id %s" , nodeAName , batchID )
131123
132- ch , close , err := listenWebsocket (ctx , nodeB . Host () , testTopic , c .logger )
124+ ch , closer , err := wslistener . ListenWebSocket (ctx , nodeB , fmt . Sprintf ( "/pss/subscribe/%s" , testTopic ) , c .logger )
133125 if err != nil {
134- cancel ()
135126 return err
136127 }
137128
138129 c .logger .Infof ("pss: sending test data to node %s and listening on node %s" , nodeAName , nodeBName )
139130
131+ defer closer ()
132+
140133 tStart := time .Now ()
141134 err = nodeA .SendPSSMessage (ctx , addrB .Overlay , addrB .PSSPublicKey , testTopic , o .AddressPrefix , testData , batchID )
142135 if err != nil {
143- close ()
144- cancel ()
145136 return err
146137 }
138+ c .logger .Infof ("pss: test data sent successfully to node %s. Waiting for response from node %s" , nodeAName , nodeBName )
147139
148- msg , ok := <- ch
149- if ok {
150- if msg == string (testData ) {
151- c .logger .Info ("pss: websocket connection received correct message" )
152- c .metrics .SendAndReceiveGauge .WithLabelValues (nodeAName , nodeBName ).Set (time .Since (tStart ).Seconds ())
153- } else {
154- err = errDataMismatch
140+ for {
141+ select {
142+ case <- time .After (1 * time .Minute ):
143+ return fmt .Errorf ("correct message not received after %s" , 1 * time .Minute )
144+ case msg , ok := <- ch :
145+ if ! ok {
146+ return fmt .Errorf ("ws closed before receiving correct message" )
147+ }
148+
149+ if msg == string (testData ) {
150+ c .logger .Info ("pss: websocket connection received correct message" )
151+ c .metrics .SendAndReceiveGauge .WithLabelValues (nodeAName , nodeBName ).Set (time .Since (tStart ).Seconds ())
152+ return nil
153+ }
154+ c .logger .Infof ("pss: received incorrect message. trying again. want %s, got %s" , string (testData ), msg )
155155 }
156- } else {
157- err = errWebsocketConnection
158- }
159-
160- cancel ()
161- close ()
162-
163- if err != nil {
164- return err
165156 }
166-
167- return nil
168- }
169-
170- func listenWebsocket (ctx context.Context , host string , topic string , logger logging.Logger ) (<- chan string , func (), error ) {
171- dialer := & websocket.Dialer {
172- Proxy : http .ProxyFromEnvironment ,
173- HandshakeTimeout : 45 * time .Second ,
174- }
175-
176- ws , _ , err := dialer .DialContext (ctx , fmt .Sprintf ("ws://%s/pss/subscribe/%s" , host , topic ), http.Header {})
177- if err != nil {
178- return nil , nil , err
179- }
180-
181- ch := make (chan string )
182-
183- go func () {
184- _ , data , err := ws .ReadMessage ()
185- if err != nil {
186- logger .Infof ("pss: websocket error %v" , err )
187- close (ch )
188- return
189- }
190-
191- ch <- string (data )
192- }()
193-
194- return ch , func () { ws .Close () }, nil
195157}
0 commit comments