graphvent/graph_test.go

61 lines
1.5 KiB
Go

2023-06-18 18:33:17 -06:00
package graphvent
2023-04-08 15:23:40 -06:00
import (
"testing"
"runtime/debug"
2024-03-04 17:30:42 -07:00
"time"
2023-06-22 15:50:42 -06:00
badger "github.com/dgraph-io/badger/v3"
2023-04-08 15:23:40 -06:00
)
2023-08-31 22:31:29 -06:00
func NewSimpleListener(ctx *Context, buffer int) (*Node, *ListenerExt, error) {
2023-07-27 00:30:24 -06:00
listener_extension := NewListenerExt(buffer)
2023-08-31 22:31:29 -06:00
listener, err := NewNode(ctx,
nil,
2024-03-03 15:45:45 -07:00
"LockableListener",
10,
nil,
2023-07-27 11:33:11 -06:00
listener_extension,
NewLockableExt(nil))
2023-07-27 00:30:24 -06:00
2023-08-31 22:31:29 -06:00
return listener, listener_extension, err
2023-07-27 00:30:24 -06:00
}
2023-07-25 21:43:15 -06:00
func logTestContext(t * testing.T, components []string) *Context {
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
if err != nil {
t.Fatal(err)
}
2023-07-25 21:43:15 -06:00
ctx, err := NewContext(db, NewConsoleLogger(components))
fatalErr(t, err)
2023-07-27 00:30:24 -06:00
err = RegisterNodeType(ctx, "LockableListener", []ExtType{ExtTypeFor[ListenerExt](), ExtTypeFor[LockableExt]()})
2023-07-27 00:30:24 -06:00
fatalErr(t, err)
2023-07-25 21:43:15 -06:00
return ctx
}
2023-07-09 14:30:30 -06:00
func testContext(t * testing.T) * Context {
2023-07-27 00:30:24 -06:00
return logTestContext(t, []string{})
}
func fatalErr(t * testing.T, err error) {
if err != nil {
debug.PrintStack()
t.Fatal(err)
}
}
2024-03-04 17:30:42 -07:00
func testSend(t *testing.T, ctx *Context, signal Signal, source, destination *Node) (ResponseSignal, []Signal) {
source_listener, err := GetExt[ListenerExt](source)
fatalErr(t, err)
messages := []SendMsg{{destination.ID, signal}}
fatalErr(t, ctx.Send(source, messages))
response, signals, err := WaitForResponse(source_listener.Chan, time.Millisecond*10, signal.ID())
fatalErr(t, err)
return response, signals
}