From e205f75fa85a522bf74e369a8abc89bbe2ecb6e7 Mon Sep 17 00:00:00 2001 From: Andreas Linde Date: Wed, 3 Jan 2024 22:13:37 +0100 Subject: [PATCH] Improve CI testing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don’t ignore the tests completely, but don’t test via time sleep triggers but calling the corresponding methods directly --- ship/handshake.go | 3 +-- ship/helper_test.go | 7 ++----- ship/hs_hello.go | 5 +++++ ship/hs_hello_test.go | 20 ++++++++++++++------ ship/websocket.go | 30 +++++++++++++++++------------- ship/websocket_test.go | 11 +++++++---- 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/ship/handshake.go b/ship/handshake.go index 3df62c64..b68522ab 100644 --- a/ship/handshake.go +++ b/ship/handshake.go @@ -135,8 +135,7 @@ func (c *ShipConnection) handleState(timeout bool, message []byte) { case SmeHelloStateReadyListen: if timeout { - c.setState(SmeHelloStateAbort, nil) - c.handleState(false, nil) + c.handshakeHello_ReadyTimeout() return } diff --git a/ship/helper_test.go b/ship/helper_test.go index 5980fefd..ba860071 100644 --- a/ship/helper_test.go +++ b/ship/helper_test.go @@ -2,11 +2,8 @@ package ship import ( "os" - "testing" ) -func skipCI(t *testing.T) { - if os.Getenv("ACTION_ENVIRONMENT") == "CI" { - t.Skip("Skipping testing in CI environment") - } +func isRunningOnCI() bool { + return os.Getenv("ACTION_ENVIRONMENT") == "CI" } diff --git a/ship/hs_hello.go b/ship/hs_hello.go index eb9cb57b..be7a30d8 100644 --- a/ship/hs_hello.go +++ b/ship/hs_hello.go @@ -76,6 +76,11 @@ func (c *ShipConnection) handshakeHello_ReadyListen(message []byte) { c.handleState(false, nil) } +func (c *ShipConnection) handshakeHello_ReadyTimeout() { + c.setState(SmeHelloStateAbort, nil) + c.handleState(false, nil) +} + // SME_HELLO_ABORT func (c *ShipConnection) handshakeHello_Abort() { c.stopHandshakeTimer() diff --git a/ship/hs_hello_test.go b/ship/hs_hello_test.go index 4c5cff75..4b22ec07 100644 --- a/ship/hs_hello_test.go +++ b/ship/hs_hello_test.go @@ -70,14 +70,18 @@ func (s *HelloSuite) Test_ReadyListen_Ok() { } func (s *HelloSuite) Test_ReadyListen_Timeout() { - skipCI(s.T()) - sut, data := initTest(s.role) sut.setState(SmeHelloStateReadyInit, nil) // inits the timer sut.setState(SmeHelloStateReadyListen, nil) - time.Sleep(tHelloInit + time.Second) + if !isRunningOnCI() { + // test if the function is triggered correctly via the timer + time.Sleep(tHelloInit + time.Second) + } else { + // speed up the test by running the method directly + sut.handshakeHello_ReadyTimeout() + } assert.Equal(s.T(), SmeHelloStateAbortDone, sut.getState()) assert.NotNil(s.T(), data.lastMessage()) @@ -157,14 +161,18 @@ func (s *HelloSuite) Test_PendingListen() { } func (s *HelloSuite) Test_PendingListen_Timeout() { - skipCI(s.T()) - sut, data := initTest(s.role) sut.setState(SmeHelloStatePendingInit, nil) // inits the timer sut.setState(SmeHelloStatePendingListen, nil) - time.Sleep(tHelloInit + time.Second) + if !isRunningOnCI() { + // test if the function is triggered correctly via the timer + time.Sleep(tHelloInit + time.Second) + } else { + // speed up the test by running the method directly + sut.handshakeHello_PendingTimeout() + } assert.Equal(s.T(), SmeHelloStateAbortDone, sut.getState()) assert.NotNil(s.T(), data.lastMessage()) diff --git a/ship/websocket.go b/ship/websocket.go index 4b24dc49..f8838f06 100644 --- a/ship/websocket.go +++ b/ship/websocket.go @@ -134,23 +134,27 @@ func (w *websocketConnection) writeShipPump() { logging.Log.Trace("Send:", w.remoteSki, text) case <-ticker.C: - if w.isConnClosed() { - return - } - - w.muxConWrite.Lock() - _ = w.conn.SetWriteDeadline(time.Now().Add(writeWait)) - w.muxConWrite.Unlock() - if err := w.writeMessage(websocket.PingMessage, nil); err != nil { - logging.Log.Debug(w.remoteSki, "error writing to websocket: ", err) - w.setConnClosedError(err) - w.dataProcessing.ReportConnectionError(err) - return - } + w.handlePing() } } } +func (w *websocketConnection) handlePing() { + if w.isConnClosed() { + return + } + + w.muxConWrite.Lock() + _ = w.conn.SetWriteDeadline(time.Now().Add(writeWait)) + w.muxConWrite.Unlock() + if err := w.writeMessage(websocket.PingMessage, nil); err != nil { + logging.Log.Debug(w.remoteSki, "error writing to websocket: ", err) + w.setConnClosedError(err) + w.dataProcessing.ReportConnectionError(err) + return + } +} + // readShipPump checks for messages from the websocket connection func (w *websocketConnection) readShipPump() { _ = w.conn.SetReadDeadline(time.Now().Add(pongWait)) diff --git a/ship/websocket_test.go b/ship/websocket_test.go index 06431ac0..53fd3f9c 100644 --- a/ship/websocket_test.go +++ b/ship/websocket_test.go @@ -93,14 +93,17 @@ func (s *WebsocketSuite) TestConnectionClose() { } func (s *WebsocketSuite) TestPingPeriod() { - skipCI(s.T()) - isClosed, err := s.sut.IsDataConnectionClosed() assert.Equal(s.T(), false, isClosed) assert.Nil(s.T(), err) - // make sure we have enough time to read and write - time.Sleep(time.Second * 51) + if !isRunningOnCI() { + // test if the function is triggered correctly via the timer + time.Sleep(time.Second * 51) + } else { + // speed up the test by running the method directly + s.sut.handlePing() + } isClosed, err = s.sut.IsDataConnectionClosed() assert.Equal(s.T(), false, isClosed)