graphvent/thread_test.go

89 lines
2.2 KiB
Go

2023-06-23 21:21:14 -06:00
package graphvent
import (
"testing"
"time"
"fmt"
"encoding/json"
2023-06-23 21:21:14 -06:00
)
func TestNewThread(t * testing.T) {
2023-06-23 21:21:14 -06:00
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) {
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)
err = UseStates(ctx, []GraphNode{t1}, func(states NodeStateMap) (error) {
owner := states[t1.ID()].(ThreadState).Owner()
if owner != nil {
return fmt.Errorf("Wrong owner %+v", owner)
}
return nil
})
2023-06-23 21:21:14 -06:00
}
2023-06-24 19:48:59 -06:00
func TestThreadWithRequirement(t * testing.T) {
ctx := testContext(t)
2023-06-24 19:48:59 -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)
SendUpdate(ctx, t1, CancelSignal(nil))
}(t1)
fatalErr(t, err)
err = RunThread(ctx, t1)
fatalErr(t, err)
err = UseStates(ctx, []GraphNode{l1}, func(states NodeStateMap) (error) {
owner := states[l1.ID()].(LockableState).Owner()
if owner != nil {
return fmt.Errorf("Wrong owner %+v", owner)
}
return nil
2023-06-24 19:48:59 -06:00
})
fatalErr(t, err)
}
func TestThreadDBLoad(t * testing.T) {
ctx := logTestContext(t, []string{})
l1, err := NewSimpleBaseLockable(ctx, "Test Lockable 1", []Lockable{})
fatalErr(t, err)
t1, err := NewSimpleBaseThread(ctx, "Test Thread 1", []Lockable{l1}, ThreadActions{}, ThreadHandlers{})
fatalErr(t, err)
SendUpdate(ctx, t1, CancelSignal(nil))
err = RunThread(ctx, t1)
fatalErr(t, err)
err = UseStates(ctx, []GraphNode{t1}, func(states NodeStateMap) error {
ser, err := json.MarshalIndent(states[t1.ID()], "", " ")
fmt.Printf("\n%s\n\n", ser)
return err
})
t1_loaded, err := LoadNode(ctx, t1.ID())
fatalErr(t, err)
err = UseStates(ctx, []GraphNode{t1_loaded}, func(states NodeStateMap) error {
ser, err := json.MarshalIndent(states[t1_loaded.ID()], "", " ")
fmt.Printf("\n%s\n\n", ser)
return err
})
}