NOISSUE - Improve reliability of state machine test (#260)

* add sleep to prevent test failing

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* add coverage

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* use codecov

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* create dir

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

---------

Signed-off-by: Sammy Oina <sammyoina@gmail.com>
This commit is contained in:
Sammy Kerata Oina
2024-09-26 12:59:26 +03:00
committed by GitHub
parent 6c4819563c
commit c69dcd0e2d
4 changed files with 36 additions and 13 deletions
+14 -1
View File
@@ -30,5 +30,18 @@ jobs:
run: |
make
- name: Create coverage directory
run: |
mkdir coverage
- name: Run tests
run: go test -v --race -covermode=atomic -coverprofile cover.out ./...
run: go test -v --race -covermode=atomic -coverprofile coverage/cover.txt ./...
- name: Upload results to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./coverage/
name: codecov-umbrella
verbose: true
+4
View File
@@ -1,4 +1,8 @@
# Cocos AI
[![codecov](https://codecov.io/gh/ultravioletrs/cocos/graph/badge.svg?token=HX01LR01K9)](https://codecov.io/gh/ultravioletrs/cocos)
![Go report card](https://goreportcard.com/badge/github.com/ultravioletrs/cocos)
[Cocos AI (Confdential Computing System for AI/ML)][cocos] is a platform for secure multiparty computation (SMPC)
based on the [Confidential Computing][cc] and [Trusted Execution Environments (TEEs)][tee].
+17 -11
View File
@@ -6,6 +6,7 @@ import (
"context"
"fmt"
"testing"
"time"
mglog "github.com/absmach/magistrala/logger"
)
@@ -39,19 +40,20 @@ func TestStateMachineTransitions(t *testing.T) {
t.Run(fmt.Sprintf("Transition from %v to %v", tc.fromState, tc.expected), func(t *testing.T) {
sm := NewStateMachine(mglog.NewMock(), tc.cmp)
ctx, cancel := context.WithCancel(context.Background())
go func() {
sm.Start(ctx)
}()
sm.wg.Wait()
sm.SetState(tc.fromState)
defer cancel()
go sm.Start(ctx)
time.Sleep(50 * time.Millisecond)
sm.SetState(tc.fromState)
sm.SendEvent(tc.event)
time.Sleep(50 * time.Millisecond)
if sm.GetState() != tc.expected {
t.Errorf("Expected state %v after the event, but got %v", tc.expected, sm.GetState())
}
close(sm.EventChan)
cancel()
})
}
}
@@ -59,14 +61,18 @@ func TestStateMachineTransitions(t *testing.T) {
func TestStateMachineInvalidTransition(t *testing.T) {
sm := NewStateMachine(mglog.NewMock(), cmp)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go sm.Start(ctx)
sm.SetState(Idle)
time.Sleep(50 * time.Millisecond)
sm.SetState(Idle)
sm.SendEvent(dataReceived)
if sm.State != Idle {
t.Errorf("State should not change on an invalid event, but got %v", sm.State)
time.Sleep(50 * time.Millisecond)
if sm.GetState() != Idle {
t.Errorf("State should not change on an invalid event, but got %v", sm.GetState())
}
cancel()
}
+1 -1
View File
@@ -27,7 +27,7 @@ import (
var _ managergrpc.Service = (*svc)(nil)
const (
svcName = "manager_test_server"
svcName = "computations_test_server"
defaultPort = "7001"
)