2023-06-23 21:21:14 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-06-23 21:57:26 -06:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-06-23 21:21:14 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewEvent(t * testing.T) {
|
|
|
|
ctx := testContext(t)
|
|
|
|
|
2023-06-25 13:39:00 -06:00
|
|
|
t1, err := NewSimpleBaseThread(ctx, "Test thread 1", []Lockable{}, ThreadActions{}, ThreadHandlers{})
|
2023-06-23 21:21:14 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
go func(thread Thread) {
|
2023-06-23 21:57:26 -06:00
|
|
|
time.Sleep(10*time.Millisecond)
|
2023-06-23 21:21:14 -06:00
|
|
|
SendUpdate(ctx, t1, CancelSignal(nil))
|
|
|
|
}(t1)
|
|
|
|
|
|
|
|
err = RunThread(ctx, t1)
|
|
|
|
fatalErr(t, err)
|
2023-06-23 21:57:26 -06:00
|
|
|
|
|
|
|
_, err = UseStates(ctx, []GraphNode{t1}, func(states []NodeState) (interface{}, error) {
|
|
|
|
ser, err := json.MarshalIndent(states, "", " ")
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
fmt.Printf("\n%s\n", ser)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
})
|
2023-06-23 21:21:14 -06:00
|
|
|
}
|
2023-06-24 19:48:59 -06:00
|
|
|
|
|
|
|
func TestEventWithRequirement(t * testing.T) {
|
|
|
|
ctx := logTestContext(t, []string{"lockable", "thread"})
|
|
|
|
|
2023-06-26 01:16:44 -06:00
|
|
|
l1, err := NewSimpleBaseLockable(ctx, "Test Lockable 1", []Lockable{})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-06-25 13:39:00 -06:00
|
|
|
t1, err := NewSimpleBaseThread(ctx, "Test Thread 1", []Lockable{l1}, ThreadActions{}, ThreadHandlers{})
|
2023-06-24 19:48:59 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
go func (thread Thread) {
|
|
|
|
time.Sleep(10*time.Millisecond)
|
|
|
|
_, err := UseStates(ctx, []GraphNode{l1}, func(states []NodeState) (interface{}, error) {
|
|
|
|
ser, err := json.MarshalIndent(states[0], "", " ")
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
fmt.Printf("\n%s\n", ser)
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
SendUpdate(ctx, t1, CancelSignal(nil))
|
|
|
|
}(t1)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
err = RunThread(ctx, t1)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
_, err = UseStates(ctx, []GraphNode{l1}, func(states []NodeState) (interface{}, error) {
|
|
|
|
ser, err := json.MarshalIndent(states[0], "", " ")
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
fmt.Printf("\n%s\n", ser)
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
2023-06-25 13:39:00 -06:00
|
|
|
|
|
|
|
func TestCustomEventState(t * testing.T ) {
|
|
|
|
ctx := logTestContext(t, []string{"lockable", "thread"})
|
|
|
|
|
|
|
|
t1, err := NewSimpleBaseThread(ctx, "Test Thread 1", []Lockable{}, ThreadActions{}, ThreadHandlers{})
|
|
|
|
fatalErr(t, err)
|
|
|
|
println(t1)
|
|
|
|
}
|