From 80a7946efc8da23b923266c6bfdd314d518b1e50 Mon Sep 17 00:00:00 2001 From: arnauds5 Date: Mon, 29 Apr 2024 12:55:29 +0200 Subject: [PATCH 1/3] Add delay to popcha test --- be1-go/channel/authentication/authentication_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/be1-go/channel/authentication/authentication_test.go b/be1-go/channel/authentication/authentication_test.go index d184a8a4af..647c239e7b 100644 --- a/be1-go/channel/authentication/authentication_test.go +++ b/be1-go/channel/authentication/authentication_test.go @@ -158,6 +158,9 @@ func Test_Authenticate_User(t *testing.T) { err = authCha.Publish(msg, socket.ClientSocket{}) require.NoError(t, err) + // The websocket server may take some time to receive and process a message + time.Sleep(time.Millisecond * 100) + require.True(t, rb.get()) } From 1dfc2bb023e90b68c6282bf3a2b080524e860bbc Mon Sep 17 00:00:00 2001 From: arnauds5 Date: Tue, 30 Apr 2024 20:33:56 +0200 Subject: [PATCH 2/3] Use channel instead of time delay --- .../authentication/authentication_test.go | 76 +++++-------------- 1 file changed, 19 insertions(+), 57 deletions(-) diff --git a/be1-go/channel/authentication/authentication_test.go b/be1-go/channel/authentication/authentication_test.go index 647c239e7b..ba1b5b3579 100644 --- a/be1-go/channel/authentication/authentication_test.go +++ b/be1-go/channel/authentication/authentication_test.go @@ -108,25 +108,17 @@ func Test_Authenticate_User(t *testing.T) { _, found := fakeHub.channelByID[name] require.True(t, found) - time.Sleep(time.Millisecond) - // Create the message file := filepath.Join(relativeMsgDataExamplePath, "popcha_authenticate", "popcha_authenticate.json") buf, err := os.ReadFile(file) require.NoError(t, err) - // struct checking that the ws server received a message from the pop backend - rb := &receivedBuffer{ - received: false, - once: sync.Once{}, - mutex: sync.Mutex{}, - } + // channel where the ws server put message it received from the pop backend + msgCh := make(chan []byte, 1) // creating dummy websocket server - newWSServer(t, "localhost:19006", rb) - - time.Sleep(time.Millisecond) + newWSServer(t, "localhost:19006", msgCh) buf64 := base64.URLEncoding.EncodeToString(buf) @@ -158,10 +150,12 @@ func Test_Authenticate_User(t *testing.T) { err = authCha.Publish(msg, socket.ClientSocket{}) require.NoError(t, err) - // The websocket server may take some time to receive and process a message - time.Sleep(time.Millisecond * 100) - - require.True(t, rb.get()) + select { + case m := <-msgCh: + require.NotNil(t, m) + case <-time.After(time.Second): + t.Errorf("No message received after 1 sec") + } } // ----------------------------------------------------------------------------- @@ -308,58 +302,26 @@ var upgrader = websocket.Upgrader{ WriteBufferSize: 1024, } -func websocketHandler(rb *receivedBuffer) func(http.ResponseWriter, *http.Request) { +func websocketHandler(t *testing.T, msgCh chan []byte) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { upgrader.CheckOrigin = func(r *http.Request) bool { return true } conn, err := upgrader.Upgrade(w, r, nil) - if err != nil { - log.Error().Msgf("error while upgrading: %v", err) - return - } + require.NoError(t, err) + + // receive the message and send it to the message channel + _, msg, err := conn.ReadMessage() + require.NoError(t, err) - // mark the boolean received as true - rb.recv() + msgCh <- msg conn.Close() } } -func newWSServer(t *testing.T, addr string, rb *receivedBuffer) { - http.HandleFunc("/", websocketHandler(rb)) +func newWSServer(t *testing.T, addr string, msgCh chan []byte) { + http.HandleFunc("/", websocketHandler(t, msgCh)) go func() { err := http.ListenAndServe(addr, nil) - if err != nil { - require.NoError(t, err) - } + require.NoError(t, err) }() } - -/* - Atomic Read/Write boolean, to check that the dummy WS server correctly received the request - -*/ - -// receivedBuffer contains a Mutex, a "once" object allowing to perform one flip only, and the boolean -type receivedBuffer struct { - received bool - once sync.Once - mutex sync.Mutex -} - -// recv flips the boolean. It can only be done once -func (rb *receivedBuffer) recv() { - rb.once.Do(func() { - rb.mutex.Lock() - defer rb.mutex.Unlock() - if !rb.received { - rb.received = true - } - }) -} - -// get performs an atomic read on the boolean -func (rb *receivedBuffer) get() bool { - rb.mutex.Lock() - defer rb.mutex.Unlock() - return rb.received -} From 57df09b8114cf0532a9887766ced74512a0adbf9 Mon Sep 17 00:00:00 2001 From: arnauds5 Date: Wed, 1 May 2024 14:08:07 +0200 Subject: [PATCH 3/3] Use channel to signal ws server started listening --- .../authentication/authentication_test.go | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/be1-go/channel/authentication/authentication_test.go b/be1-go/channel/authentication/authentication_test.go index ba1b5b3579..3a1cf90ebf 100644 --- a/be1-go/channel/authentication/authentication_test.go +++ b/be1-go/channel/authentication/authentication_test.go @@ -14,6 +14,7 @@ import ( "golang.org/x/sync/semaphore" "golang.org/x/xerrors" "io" + "net" "net/http" "os" "path/filepath" @@ -114,11 +115,16 @@ func Test_Authenticate_User(t *testing.T) { buf, err := os.ReadFile(file) require.NoError(t, err) - // channel where the ws server put message it received from the pop backend + // channel where the ws server signals when it has started listening msgCh := make(chan []byte, 1) + // channel where the ws server put message it received from the pop backend + startCh := make(chan struct{}, 1) // creating dummy websocket server - newWSServer(t, "localhost:19006", msgCh) + newWSServer(t, "localhost:19006", msgCh, startCh) + + // wait that the ws server has started listening + <-startCh buf64 := base64.URLEncoding.EncodeToString(buf) @@ -318,10 +324,16 @@ func websocketHandler(t *testing.T, msgCh chan []byte) func(http.ResponseWriter, } } -func newWSServer(t *testing.T, addr string, msgCh chan []byte) { +func newWSServer(t *testing.T, addr string, msgCh chan []byte, startCh chan struct{}) { http.HandleFunc("/", websocketHandler(t, msgCh)) go func() { - err := http.ListenAndServe(addr, nil) + listener, err := net.Listen("tcp", addr) + require.NoError(t, err) + + // signal that the ws server has started listening + startCh <- struct{}{} + + err = http.Serve(listener, nil) require.NoError(t, err) }() }