"github.com/stretchr/testify/require"
+ "onehouse/driver"
"onehouse/protocol"
)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
- d, client, _ := testAssembly(t)
+ d, client, _, router := testAssembly(t)
settings := Settings{
DeviceID: testDeviceID,
Address: "example.com:502",
}
tc.change(&settings)
- _, err := New(settings, d.publisher, client)
+ _, err := New(settings, d.publisher, router, client)
require.Error(t, err)
})
}
+
+ d, client, _, _ := testAssembly(t)
+ router, err := driver.NewRouter(nil)
+ require.NoError(t, err)
+
+ _, err = New(Settings{
+ DeviceID: testDeviceID,
+ Address: "example.com:502",
+ Unit: 1,
+ PollInterval: 50 * time.Millisecond,
+ Modules: []Module{{Model: HostModel}},
+ }, d.publisher, router, client)
+ require.ErrorContains(t, err, "command queue")
}
func TestDriverReconnectsWithConfigurationAndFreshBaseline(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
- d, client, transport := testAssembly(t)
+ d, client, transport, router := testAssembly(t)
client.connectErrors = []error{errors.New("unavailable")}
client.inputs[1] = true
ctx, cancel := context.WithCancel(t.Context())
})
require.Empty(t, transport.ofKind(protocol.Event))
+ command := testCommand(t, "m0.do0", "true")
+ queueCommands(t, router, []driver.Command{command})
+ synctest.Wait()
+
+ record, exists := d.ledger.Lookup(command.Metadata.MessageID)
+ require.True(t, exists)
+ require.Equal(t, "failed", record.Payload.Status)
+ client.withLock(func(client *fakeClient) {
+ require.Empty(t, client.writes)
+ })
time.Sleep(time.Second)
synctest.Wait()
client.withLock(func(client *fakeClient) {
require.Len(t, client.registerWrites, 66)
})
require.Len(t, transport.ofKind(protocol.Event), 1)
+ queueCommands(t, router, []driver.Command{command})
+ synctest.Wait()
+ client.withLock(func(client *fakeClient) {
+ require.Empty(t, client.writes)
+ })
+
+ command = testCommand(t, "m1.do15", "true")
+ queueCommands(t, router, []driver.Command{command})
+ synctest.Wait()
+ client.withLock(func(client *fakeClient) {
+ require.Len(t, client.writes, 1)
+ })
cancel()
require.ErrorIs(t, <-done, context.Canceled)
client.withLock(func(client *fakeClient) {
func TestDriverBoundsReconnectBackoff(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
- d, client, _ := testAssembly(t)
+ d, client, _, _ := testAssembly(t)
for range 8 {
client.withLock(func(client *fakeClient) {
client.connectErrors = append(client.connectErrors, errors.New("offline"))
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
- d, client, transport := testAssembly(t)
+ d, client, transport, _ := testAssembly(t)
failure := errors.New("independent failure")
if tc.closeFailure {
client.closeErr = failure
func TestDriverRefreshDoesNotRepeatCommandsOrEvents(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
- d, client, transport := testAssembly(t)
+ d, client, transport, _ := testAssembly(t)
ctx, cancel := context.WithCancel(t.Context())
defer cancel()