Skip to content

Commit

Permalink
Add exeuction consensus on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert committed Mar 5, 2020
1 parent 8554d14 commit e3269c4
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 122 deletions.
11 changes: 6 additions & 5 deletions execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// New returns a new execution. It returns an error if inputs are invalid.
func New(processHash, instanceHash, parentHash, eventHash hash.Hash, nodeKey, taskKey, price string, inputs *types.Struct, tags []string, executorHash hash.Hash) *Execution {
func New(processHash, instanceHash, parentHash, eventHash hash.Hash, nodeKey, taskKey, price string, inputs *types.Struct, tags []string, executorHash hash.Hash, confs int64) *Execution {
exec := &Execution{
ProcessHash: processHash,
EventHash: eventHash,
Expand All @@ -17,19 +17,20 @@ func New(processHash, instanceHash, parentHash, eventHash hash.Hash, nodeKey, ta
NodeKey: nodeKey,
Tags: tags,
Price: price,
Status: Status_Created,
Status: Status_Voting,
ExecutorHash: executorHash,
Confs: confs,
}
exec.Hash = hash.Dump(exec)
return exec
}

// Execute changes executions status to in progres and update its execute time.
// It returns an error if the status is different then Created.
// It returns an error if the status is different then Passed.
func (execution *Execution) Execute() error {
if execution.Status != Status_Created {
if execution.Status != Status_Passed {
return StatusError{
ExpectedStatus: Status_Created,
ExpectedStatus: Status_Passed,
ActualStatus: execution.Status,
}
}
Expand Down
98 changes: 54 additions & 44 deletions execution/execution.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ func TestNewFromService(t *testing.T) {
tags = []string{"tag"}
)

execution := New(nil, hash, parentHash, eventHash, "", taskKey, "", nil, tags, nil)
execution := New(nil, hash, parentHash, eventHash, "", taskKey, "", nil, tags, nil, 0)
require.NotNil(t, execution)
require.Equal(t, hash, execution.InstanceHash)
require.Equal(t, parentHash, execution.ParentHash)
require.Equal(t, eventHash, execution.EventHash)
require.Equal(t, taskKey, execution.TaskKey)
require.Equal(t, (*types.Struct)(nil), execution.Inputs)
require.Equal(t, tags, execution.Tags)
require.Equal(t, Status_Created, execution.Status)
require.Equal(t, Status_Voting, execution.Status)
}

func TestExecute(t *testing.T) {
e := New(nil, nil, nil, nil, "", "", "", nil, nil, nil)
e := New(nil, nil, nil, nil, "", "", "", nil, nil, nil, 0)
e.Status = Status_Passed
require.NoError(t, e.Execute())
require.Equal(t, Status_InProgress, e.Status)
require.Error(t, e.Execute())
}

func TestComplete(t *testing.T) {
var output types.Struct
e := New(nil, nil, nil, nil, "", "", "", nil, nil, nil)
e := New(nil, nil, nil, nil, "", "", "", nil, nil, nil, 0)
e.Status = Status_Passed
e.Execute()
require.NoError(t, e.Complete(&output))
require.Equal(t, Status_Completed, e.Status)
Expand All @@ -49,7 +51,8 @@ func TestComplete(t *testing.T) {

func TestFailed(t *testing.T) {
err := errors.New("test")
e := New(nil, nil, nil, nil, "", "", "", nil, nil, nil)
e := New(nil, nil, nil, nil, "", "", "", nil, nil, nil, 0)
e.Status = Status_Passed
e.Execute()
require.NoError(t, e.Failed(err))
require.Equal(t, Status_Failed, e.Status)
Expand All @@ -61,7 +64,7 @@ func TestExecutionHash(t *testing.T) {
ids := make(map[string]bool)

f := func(instanceHash, parentHash, eventID []byte, taskKey string, tags []string) bool {
e := New(nil, instanceHash, parentHash, eventID, "", taskKey, "", nil, tags, nil)
e := New(nil, instanceHash, parentHash, eventID, "", taskKey, "", nil, tags, nil, 0)
if ids[string(e.Hash)] {
return false
}
Expand Down
Loading

0 comments on commit e3269c4

Please sign in to comment.