Skip to content

Commit

Permalink
Fix chaos
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Nov 18, 2020
1 parent 7ce4e53 commit 4982de8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
11 changes: 11 additions & 0 deletions conformance/chaos/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const (
// MethodInspectRuntime is the identifier for the method that returns the
// current runtime values.
MethodInspectRuntime
// MethodCreateState is the identifier for the method that creates the chaos actor's state.
MethodCreateState
)

// Exports defines the methods this actor exposes publicly.
Expand All @@ -87,6 +89,7 @@ func (a Actor) Exports() []interface{} {
MethodMutateState: a.MutateState,
MethodAbortWith: a.AbortWith,
MethodInspectRuntime: a.InspectRuntime,
MethodCreateState: a.CreateState,
}
}

Expand Down Expand Up @@ -227,6 +230,14 @@ type MutateStateArgs struct {
Branch MutateStateBranch
}

// CreateState creates the chaos actor's state
func (a Actor) CreateState(rt runtime2.Runtime, _ *abi.EmptyValue) *abi.EmptyValue {
rt.ValidateImmediateCallerAcceptAny()
rt.StateCreate(&State{})

return nil
}

// MutateState attempts to mutate a state value in the actor.
func (a Actor) MutateState(rt runtime2.Runtime, args *MutateStateArgs) *abi.EmptyValue {
rt.ValidateImmediateCallerAcceptAny()
Expand Down
64 changes: 41 additions & 23 deletions conformance/chaos/actor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ func TestMutateStateInTransaction(t *testing.T) {
var a Actor

rt.ExpectValidateCallerAny()
rt.StateCreate(&State{})
rt.Call(a.CreateState, nil)

rt.ExpectValidateCallerAny()
val := "__mutstat test"
rt.Call(a.MutateState, &MutateStateArgs{
Value: val,
Expand All @@ -155,23 +156,30 @@ func TestMutateStateAfterTransaction(t *testing.T) {
var a Actor

rt.ExpectValidateCallerAny()
rt.StateCreate(&State{})
rt.Call(a.CreateState, nil)

rt.ExpectValidateCallerAny()
val := "__mutstat test"
defer func() {
if r := recover(); r == nil {
t.Fatal("The code did not panic")
} else {
var st State
rt.GetState(&st)

// state should be updated successfully _in_ the transaction but not outside
if st.Value != val+"-in" {
t.Fatal("state was not updated")
}

rt.Verify()
}
}()
rt.Call(a.MutateState, &MutateStateArgs{
Value: val,
Branch: MutateAfterTransaction,
})

var st State
rt.GetState(&st)

// state should be updated successfully _in_ the transaction but not outside
if st.Value != val+"-in" {
t.Fatal("state was not updated")
}

rt.Verify()
}

func TestMutateStateReadonly(t *testing.T) {
Expand All @@ -182,22 +190,30 @@ func TestMutateStateReadonly(t *testing.T) {
var a Actor

rt.ExpectValidateCallerAny()
rt.StateCreate(&State{})
rt.Call(a.CreateState, nil)

rt.ExpectValidateCallerAny()
val := "__mutstat test"
defer func() {
if r := recover(); r == nil {
t.Fatal("The code did not panic")
} else {
var st State
rt.GetState(&st)

if st.Value != "" {
t.Fatal("state was not expected to be updated")
}

rt.Verify()
}
}()

rt.Call(a.MutateState, &MutateStateArgs{
Value: val,
Branch: MutateReadonly,
})

var st State
rt.GetState(&st)

if st.Value != "" {
t.Fatal("state was not expected to be updated")
}

rt.Verify()
}

func TestMutateStateInvalidBranch(t *testing.T) {
Expand Down Expand Up @@ -254,11 +270,13 @@ func TestInspectRuntime(t *testing.T) {
receiver := atesting2.NewIDAddr(t, 101)
builder := mock2.NewBuilder(context.Background(), receiver)

rt := builder.Build(t)
rt.SetCaller(caller, builtin2.AccountActorCodeID)
rt.StateCreate(&State{})
var a Actor

rt := builder.Build(t)
rt.ExpectValidateCallerAny()
rt.Call(a.CreateState, nil)

rt.SetCaller(caller, builtin2.AccountActorCodeID)
rt.ExpectValidateCallerAny()
ret := rt.Call(a.InspectRuntime, abi.Empty)
rtr, ok := ret.(*InspectRuntimeReturn)
Expand Down

0 comments on commit 4982de8

Please sign in to comment.