2023-06-18 18:33:17 -06:00
|
|
|
package graphvent
|
2023-04-08 15:23:40 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-05-29 23:54:52 -06:00
|
|
|
"fmt"
|
2023-06-23 10:10:25 -06:00
|
|
|
"time"
|
2023-06-03 18:56:14 -06:00
|
|
|
"runtime/pprof"
|
2023-07-23 17:57:47 -06:00
|
|
|
"runtime/debug"
|
2023-06-23 10:10:25 -06:00
|
|
|
"os"
|
2023-06-22 15:50:42 -06:00
|
|
|
badger "github.com/dgraph-io/badger/v3"
|
2023-04-08 15:23:40 -06:00
|
|
|
)
|
|
|
|
|
2023-06-18 18:39:52 -06:00
|
|
|
type GraphTester testing.T
|
2023-06-02 17:31:29 -06:00
|
|
|
const listner_timeout = 50 * time.Millisecond
|
2023-04-08 15:23:40 -06:00
|
|
|
|
2023-07-27 18:08:43 -06:00
|
|
|
func (t * GraphTester) WaitForLinkState(ctx * Context, listener *ListenerExt, state string, timeout time.Duration, str string) Signal {
|
|
|
|
timeout_channel := time.After(timeout)
|
|
|
|
for true {
|
|
|
|
select {
|
|
|
|
case signal := <- listener.Chan:
|
|
|
|
if signal == nil {
|
|
|
|
ctx.Log.Logf("test", "SIGNAL_CHANNEL_CLOSED: %s", listener)
|
|
|
|
t.Fatal(str)
|
|
|
|
}
|
|
|
|
if signal.Type() == LinkSignalType {
|
|
|
|
sig, ok := signal.(LinkSignal)
|
|
|
|
if ok == true {
|
|
|
|
ctx.Log.Logf("test", "Link state received: %s", sig.State)
|
|
|
|
if sig.State == state {
|
|
|
|
return signal
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.Log.Logf("test", "Failed to cast signal to LinkSignal: %+v", signal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-timeout_channel:
|
|
|
|
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
|
|
|
t.Fatal(str)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:48:39 -06:00
|
|
|
func (t * GraphTester) WaitForStatus(ctx * Context, listener *ListenerExt, status string, timeout time.Duration, str string) Signal {
|
2023-06-03 02:07:16 -06:00
|
|
|
timeout_channel := time.After(timeout)
|
|
|
|
for true {
|
|
|
|
select {
|
2023-07-27 16:48:39 -06:00
|
|
|
case signal := <- listener.Chan:
|
2023-06-24 19:48:59 -06:00
|
|
|
if signal == nil {
|
|
|
|
ctx.Log.Logf("test", "SIGNAL_CHANNEL_CLOSED: %s", listener)
|
|
|
|
t.Fatal(str)
|
|
|
|
}
|
2023-07-23 21:14:15 -06:00
|
|
|
if signal.Type() == "status" {
|
|
|
|
sig, ok := signal.(StatusSignal)
|
|
|
|
if ok == true {
|
2023-07-27 16:48:39 -06:00
|
|
|
|
|
|
|
|
2023-07-27 00:30:24 -06:00
|
|
|
ctx.Log.Logf("test", "Status received: %s", sig.Status)
|
2023-07-23 21:14:15 -06:00
|
|
|
if sig.Status == status {
|
|
|
|
return signal
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.Log.Logf("test", "Failed to cast status to StatusSignal: %+v", signal)
|
|
|
|
}
|
2023-06-03 02:07:16 -06:00
|
|
|
}
|
|
|
|
case <-timeout_channel:
|
2023-06-03 18:56:14 -06:00
|
|
|
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
2023-06-03 02:07:16 -06:00
|
|
|
t.Fatal(str)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:48:39 -06:00
|
|
|
func (t * GraphTester) CheckForNone(listener *ListenerExt, str string) {
|
2023-04-08 15:23:40 -06:00
|
|
|
timeout := time.After(listner_timeout)
|
|
|
|
select {
|
2023-07-27 16:48:39 -06:00
|
|
|
case sig := <- listener.Chan:
|
2023-06-03 18:56:14 -06:00
|
|
|
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
|
|
|
t.Fatal(fmt.Sprintf("%s : %+v", str, sig))
|
2023-06-03 01:38:35 -06:00
|
|
|
case <-timeout:
|
2023-04-08 15:23:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 00:30:24 -06:00
|
|
|
const SimpleListenerNodeType = NodeType("SIMPLE_LISTENER")
|
|
|
|
|
|
|
|
func NewSimpleListener(ctx *Context, buffer int) (*Node, *ListenerExt) {
|
2023-07-27 18:08:43 -06:00
|
|
|
policy := NewAllNodesPolicy([]SignalType{SignalType("status")})
|
2023-07-27 00:30:24 -06:00
|
|
|
listener_extension := NewListenerExt(buffer)
|
2023-07-27 11:33:11 -06:00
|
|
|
listener := NewNode(ctx,
|
|
|
|
RandID(),
|
|
|
|
SimpleListenerNodeType,
|
2023-07-27 15:27:14 -06:00
|
|
|
nil,
|
2023-07-27 11:33:11 -06:00
|
|
|
listener_extension,
|
|
|
|
NewACLExt(&policy),
|
|
|
|
NewLockableExt(nil, nil, nil, nil))
|
2023-07-27 00:30:24 -06:00
|
|
|
|
|
|
|
return listener, listener_extension
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func logTestContext(t * testing.T, components []string) *Context {
|
2023-06-23 10:10:25 -06:00
|
|
|
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
|
2023-06-20 22:36:18 -06:00
|
|
|
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
|
|
|
|
2023-07-27 09:32:33 -06:00
|
|
|
err = ctx.RegisterNodeType(SimpleListenerNodeType, []ExtType{ACLExtType, ListenerExtType, LockableExtType})
|
2023-07-27 00:30:24 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
return ctx
|
2023-06-20 22:36:18 -06:00
|
|
|
}
|
|
|
|
|
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{})
|
2023-06-20 20:48:48 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:10:25 -06:00
|
|
|
func fatalErr(t * testing.T, err error) {
|
2023-06-20 20:48:48 -06:00
|
|
|
if err != nil {
|
2023-07-23 17:57:47 -06:00
|
|
|
debug.PrintStack()
|
2023-06-20 20:48:48 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|