Skip to content

Commit

Permalink
Improve CI testing
Browse files Browse the repository at this point in the history
Don’t ignore the tests completely, but don’t test via time sleep triggers but calling the corresponding methods directly
  • Loading branch information
DerAndereAndi committed Jan 3, 2024
1 parent 941b724 commit e205f75
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 30 deletions.
3 changes: 1 addition & 2 deletions ship/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 2 additions & 5 deletions ship/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
5 changes: 5 additions & 0 deletions ship/hs_hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 14 additions & 6 deletions ship/hs_hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
30 changes: 17 additions & 13 deletions ship/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 7 additions & 4 deletions ship/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e205f75

Please sign in to comment.