graphvent/graph_test.go

68 lines
1.6 KiB
Go

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"
"time"
2023-06-03 18:56:14 -06:00
"runtime/pprof"
"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
const listner_timeout = 50 * time.Millisecond
2023-04-08 15:23:40 -06:00
func (t * GraphTester) WaitForValue(ctx * GraphContext, listener chan GraphSignal, signal_type string, source GraphNode, timeout time.Duration, str string) GraphSignal {
2023-06-03 02:07:16 -06:00
timeout_channel := time.After(timeout)
for true {
select {
case signal := <- listener:
if signal.Type() == signal_type {
ctx.Log.Logf("test", "SIGNAL_TYPE_FOUND: %s - %s %+v\n", signal.Type(), signal.Source(), listener)
if signal.Source() == source.ID() {
return signal
2023-06-03 18:56:14 -06:00
}
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-06-18 18:39:52 -06:00
func (t * GraphTester) CheckForNone(listener chan GraphSignal, str string) {
2023-04-08 15:23:40 -06:00
timeout := time.After(listner_timeout)
select {
2023-06-03 01:38:35 -06:00
case sig := <- listener:
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
}
}
func logTestContext(t * testing.T, components []string) * GraphContext {
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
if err != nil {
t.Fatal(err)
}
return NewGraphContext(db, NewConsoleLogger(components))
}
func testContext(t * testing.T) * GraphContext {
db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true))
2023-06-22 15:50:42 -06:00
if err != nil {
t.Fatal(err)
}
return NewGraphContext(db, NewConsoleLogger([]string{}))
}
func fatalErr(t * testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}