test: fix unpredictable test

This commit is contained in:
Cyrille Nofficial 2021-09-09 23:19:39 +02:00
parent b6f9ca4f3f
commit d6a20c8306

View File

@ -10,7 +10,6 @@ import (
"log" "log"
"sync" "sync"
"testing" "testing"
"time"
) )
type fakeVideoSource struct { type fakeVideoSource struct {
@ -37,10 +36,13 @@ func TestOpencvCameraPart(t *testing.T) {
defer func() { defer func() {
publish = oldPublish publish = oldPublish
}() }()
waitEvent := sync.WaitGroup{}
waitEvent.Add(1)
publish = func(_ mqtt.Client, topic string, payload *[]byte) { publish = func(_ mqtt.Client, topic string, payload *[]byte) {
muPubEvents.Lock() muPubEvents.Lock()
defer muPubEvents.Unlock() defer muPubEvents.Unlock()
publishedEvents[topic] = payload publishedEvents[topic] = payload
waitEvent.Done()
} }
const topic = "topic/test/camera" const topic = "topic/test/camera"
@ -50,12 +52,12 @@ func TestOpencvCameraPart(t *testing.T) {
client: nil, client: nil,
vc: fakeVideoSource{}, vc: fakeVideoSource{},
topic: topic, topic: topic,
publishFrequency: 1000, publishFrequency: 2, // Send 2 img/s for tests
imgBuffered: &imgBuffer, imgBuffered: &imgBuffer,
} }
go part.Start() go part.Start()
time.Sleep(5 * time.Millisecond) waitEvent.Wait()
var frameMsg events.FrameMessage var frameMsg events.FrameMessage
muPubEvents.Lock() muPubEvents.Lock()