Files
compute-blade-agent/pkg/eventbus/eventbus_test.go
Matthias Riegler 99920370fb feat: add smart fan unit support (#29)
* feat: add smart fanunit (serial) protocol

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* feat: add rudimentary eventbus to ease implementation

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* feat: smart fanunit client

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* feat: initial smart fan unit implementation

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* feat: improve logging, double btn press

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* fix: testcases

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* fix: context closure handling, RPM reporting

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* fix: address linting issues

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* fix: edge line closure

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* fix: reset CPU after i2c lockup

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* feat: add uf2 to release

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

---------

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>
2023-11-25 11:07:50 +01:00

77 lines
1.9 KiB
Go

package eventbus_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/xvzf/computeblade-agent/pkg/eventbus"
)
func TestEventBusManySubscribers(t *testing.T) {
eb := eventbus.New()
// Create a channel and subscribe to a topic without a filter
sub0 := eb.Subscribe("topic0", 2, eventbus.MatchAll)
assert.Equal(t, cap(sub0.C()), 2)
assert.Equal(t, len(sub0.C()), 0)
defer sub0.Unsubscribe()
// Create a channel and subscribe to a topic with a filter
sub1 := eb.Subscribe("topic0", 2, func(msg any) bool {
return msg.(int) > 5
})
assert.Equal(t, cap(sub1.C()), 2)
assert.Equal(t, len(sub1.C()), 0)
defer sub1.Unsubscribe()
// Create a channel and subscribe to another topic
sub2 := eb.Subscribe("topic1", 1, eventbus.MatchAll)
assert.Equal(t, cap(sub2.C()), 1)
assert.Equal(t, len(sub2.C()), 0)
defer sub2.Unsubscribe()
sub3 := eb.Subscribe("topic1", 0, eventbus.MatchAll)
assert.Equal(t, cap(sub3.C()), 0)
assert.Equal(t, len(sub3.C()), 0)
defer sub3.Unsubscribe()
// Publish some messages
eb.Publish("topic0", 10)
eb.Publish("topic0", 4)
eb.Publish("topic1", "Hello, World!")
// Assert received messages
assert.Equal(t, len(sub0.C()), 2)
assert.Equal(t, <-sub0.C(), 10)
assert.Equal(t, <-sub0.C(), 4)
assert.Equal(t, len(sub1.C()), 1)
assert.Equal(t, <-sub1.C(), 10)
assert.Equal(t, len(sub2.C()), 1)
assert.Equal(t, <-sub2.C(), "Hello, World!")
// sub3 has no buffer, so it should be empty as there's been no consumer at time of publishing
assert.Equal(t, len(sub3.C()), 0)
}
func TestUnsubscribe(t *testing.T) {
eb := eventbus.New()
// Create a channel and subscribe to a topic
sub := eb.Subscribe("topic", 2, eventbus.MatchAll)
// Unsubscribe from the topic
sub.Unsubscribe()
// Try to publish a message after unsubscribing
eb.Publish("topic", "This message should not be received")
// Assert that the channel is closed
_, ok := <-sub.C()
assert.False(t, ok, "Unsubscribed channel should be closed")
}