Fix chaos

This commit is contained in:
Aayush Rajasekaran 2020-11-17 17:17:08 -05:00
parent 7ce4e5342b
commit 4982de89b2
2 changed files with 52 additions and 23 deletions

View File

@ -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.
@ -87,6 +89,7 @@ func (a Actor) Exports() []interface{} {
MethodMutateState: a.MutateState,
MethodAbortWith: a.AbortWith,
MethodInspectRuntime: a.InspectRuntime,
MethodCreateState: a.CreateState,
}
}
@ -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()

View File

@ -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,
@ -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) {
@ -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) {
@ -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)