github.com/pingcap/chaos@v0.0.0-20190710112158-c86faf4b3719/pkg/model/cas_register_test.go (about)

     1  package model
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/anishathalye/porcupine"
     7  	"github.com/pingcap/chaos/pkg/core"
     8  )
     9  
    10  func convertModel(m core.Model) porcupine.Model {
    11  	return porcupine.Model{
    12  		Init:  m.Init,
    13  		Step:  m.Step,
    14  		Equal: m.Equal,
    15  	}
    16  }
    17  
    18  func TestCasRegisterModel(t *testing.T) {
    19  	events := []porcupine.Event{
    20  		{Kind: porcupine.CallEvent, Value: CasRegisterRequest{Op: CasRegisterWrite, Arg1: 100, Arg2: 0}, Id: 0},
    21  		{Kind: porcupine.CallEvent, Value: CasRegisterRequest{Op: CasRegisterRead}, Id: 1},
    22  		{Kind: porcupine.CallEvent, Value: CasRegisterRequest{Op: CasRegisterRead}, Id: 2},
    23  		{Kind: porcupine.CallEvent, Value: CasRegisterRequest{Op: CasRegisterCAS, Arg1: 100, Arg2: 200}, Id: 3},
    24  		{Kind: porcupine.CallEvent, Value: CasRegisterRequest{Op: CasRegisterRead}, Id: 4},
    25  		{Kind: porcupine.ReturnEvent, Value: CasRegisterResponse{}, Id: 2},
    26  		{Kind: porcupine.ReturnEvent, Value: CasRegisterResponse{Value: 100}, Id: 1},
    27  		{Kind: porcupine.ReturnEvent, Value: CasRegisterResponse{}, Id: 0},
    28  		{Kind: porcupine.ReturnEvent, Value: CasRegisterResponse{Ok: true}, Id: 3},
    29  		{Kind: porcupine.ReturnEvent, Value: CasRegisterResponse{Value: 200}, Id: 4},
    30  	}
    31  	res := porcupine.CheckEvents(convertModel(CasRegisterModel()), events)
    32  	if res != true {
    33  		t.Fatal("expected operations to be linearizable")
    34  	}
    35  }
    36  
    37  func TestCasRegisterModelPrepare(t *testing.T) {
    38  	model := RegisterModel()
    39  	model.Prepare(888)
    40  	state := model.Init()
    41  	if state.(int) != 888 {
    42  		t.Fatalf("expected to be 888, got %v", state)
    43  	}
    44  }