Skip to content

Commit

Permalink
Add tests for negative cases
Browse files Browse the repository at this point in the history
  • Loading branch information
xosmig committed Jun 23, 2022
1 parent ae18e0c commit cb40f96
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions pkg/dsl/dslmodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/filecoin-project/mir/pkg/events"
"github.com/filecoin-project/mir/pkg/modules"
"github.com/filecoin-project/mir/pkg/pb/eventpb"
"github.com/filecoin-project/mir/pkg/pb/messagepb"
"github.com/filecoin-project/mir/pkg/types"
"github.com/filecoin-project/mir/pkg/util/mathutil"
"github.com/filecoin-project/mir/pkg/util/sliceutil"
Expand Down Expand Up @@ -97,6 +98,13 @@ func newSimpleTestingModule(mc *simpleModuleConfig) modules.PassiveModule {
return nil
})

UponCondition(m, func() error {
if uintsSum > 100_000 {
return errors.New("too much!")
}
return nil
})

return m
}

Expand Down Expand Up @@ -145,6 +153,16 @@ func TestDslModule_ApplyEvents(t *testing.T) {
events.TestingUint(mc.Reports, 300), events.TestingUint(mc.Reports, 400),
events.TestingUint(mc.Reports, 500)),
},
"test unknown event type": {
eventsIn: events.ListOf(events.SendMessage(mc.Self, &messagepb.Message{}, []types.NodeID{})),
eventsOut: events.EmptyList(),
err: errors.New("unknown event type '*eventpb.Event_SendMessage'"),
},
"test failed condition": {
eventsIn: events.ListOf(events.TestingUint(mc.Self, 999_999_999)),
eventsOut: events.EmptyList(),
err: errors.New("too much!"),
},
}

for testName, tc := range tests {
Expand Down Expand Up @@ -270,7 +288,7 @@ func TestDslModule_ContextRecoveryAndCleanup(t *testing.T) {
testCases := map[string]func(mc *contextTestingModuleModuleConfig, m Module){
"empty": func(mc *contextTestingModuleModuleConfig, m Module) {},

"request_response": func(mc *contextTestingModuleModuleConfig, m Module) {
"request response": func(mc *contextTestingModuleModuleConfig, m Module) {
eventsOut, err := m.ApplyEvents(events.ListOf(events.TestingString(mc.Self, "hello")))
assert.Nil(t, err)
assert.Equal(t, 2, eventsOut.Len())
Expand All @@ -288,15 +306,15 @@ func TestDslModule_ContextRecoveryAndCleanup(t *testing.T) {
assert.Equal(t, []*eventpb.Event{events.TestingString(mc.Hashed, "hello: world")}, eventsOut.Slice())
},

"response_without_request": func(mc *contextTestingModuleModuleConfig, m Module) {
"response without request": func(mc *contextTestingModuleModuleConfig, m Module) {
assert.Panics(t, func() {
// Context with id 42 doesn't exist. The module should panic.
_, _ = m.ApplyEvents(events.ListOf(
events.SignResult(mc.Self, []byte{}, DslSignOrigin(mc.Self, ContextID(42)))))
})
},

"check_context_is_disposed": func(mc *contextTestingModuleModuleConfig, m Module) {
"check context is disposed": func(mc *contextTestingModuleModuleConfig, m Module) {
eventsOut, err := m.ApplyEvents(events.ListOf(events.TestingString(mc.Self, "hello")))
assert.Nil(t, err)
assert.Equal(t, 2, eventsOut.Len())
Expand All @@ -316,7 +334,7 @@ func TestDslModule_ContextRecoveryAndCleanup(t *testing.T) {
})
},

"check_multiple_handlers_for_response": func(mc *contextTestingModuleModuleConfig, m Module) {
"check multiple handlers for response": func(mc *contextTestingModuleModuleConfig, m Module) {
eventsOut, err := m.ApplyEvents(events.ListOf(events.TestingUint(mc.Self, 8)))
assert.Nil(t, err)
assert.Equal(t, 1, eventsOut.Len())
Expand Down

0 comments on commit cb40f96

Please sign in to comment.