283 lines
7.4 KiB
Go
283 lines
7.4 KiB
Go
package camfish
|
|
|
|
import "context"
|
|
import "testing"
|
|
|
|
type mockInitializable string
|
|
func (this *mockInitializable) Type() string { return string(*this) }
|
|
func (*mockInitializable) Init(ctx context.Context) error { return ctx.Err() }
|
|
|
|
func TestActorSet(test *testing.T) {
|
|
set := actorSet[Initializable] { }
|
|
|
|
// add actors
|
|
actor0 := mockInitializable("type0")
|
|
test.Logf("actor0: %p", &actor0)
|
|
set.add(&actor0)
|
|
if len(set.actors) != 1 {
|
|
test.Fatalf("wrong length: %d", len(set.actors))
|
|
}
|
|
if _, ok := set.actors[&actor0]; !ok {
|
|
test.Fatal("missing item")
|
|
}
|
|
actor1 := mockInitializable("type0")
|
|
test.Logf("actor1: %p", &actor1)
|
|
set.add(&actor1)
|
|
if len(set.actors) != 2 {
|
|
test.Fatalf("wrong length: %d", len(set.actors))
|
|
}
|
|
if _, ok := set.actors[&actor1]; !ok {
|
|
test.Fatal("missing item")
|
|
}
|
|
actor2 := mockInitializable("type1")
|
|
test.Logf("actor2: %p", &actor2)
|
|
set.add(&actor2)
|
|
if len(set.actors) != 3 {
|
|
test.Fatalf("wrong length: %d", len(set.actors))
|
|
}
|
|
if _, ok := set.actors[&actor2]; !ok {
|
|
test.Fatal("missing item")
|
|
}
|
|
|
|
// find
|
|
found := set.find("type0")
|
|
test.Log("find:", found)
|
|
if found != &actor0 && found != &actor1 {
|
|
test.Fatalf("not equal: %p", found)
|
|
}
|
|
found = set.find("type1")
|
|
test.Log("find:", found)
|
|
if found != &actor2 {
|
|
test.Fatalf("not equal: %p", found)
|
|
}
|
|
|
|
// findAll
|
|
foundAll := set.findAll("type0")
|
|
test.Log("findAll:", foundAll)
|
|
if len(foundAll) != 2 {
|
|
test.Fatalf("wrong length: %d", len(foundAll))
|
|
}
|
|
if !mockInitializableIn(foundAll, &actor0) {
|
|
test.Fatal("missing item")
|
|
}
|
|
if !mockInitializableIn(foundAll, &actor1) {
|
|
test.Fatal("missing item")
|
|
}
|
|
foundAll = set.findAll("type1")
|
|
test.Log("findAll:", foundAll)
|
|
if len(foundAll) != 1 {
|
|
test.Fatalf("wrong length: %d", len(foundAll))
|
|
}
|
|
if !mockInitializableIn(foundAll, &actor2) {
|
|
test.Fatal("missing item")
|
|
}
|
|
|
|
// all
|
|
all := set.all()
|
|
test.Log("all:", all)
|
|
if len(all) != 3 {
|
|
test.Fatalf("wrong length: %d", len(all))
|
|
}
|
|
if !mockInitializableIn(all, &actor0) {
|
|
test.Fatal("missing item")
|
|
}
|
|
if !mockInitializableIn(all, &actor1) {
|
|
test.Fatal("missing item")
|
|
}
|
|
if !mockInitializableIn(all, &actor2) {
|
|
test.Fatal("missing item")
|
|
}
|
|
|
|
// del
|
|
set.del(&actor1)
|
|
test.Log("del")
|
|
if len(set.actors) != 2 {
|
|
test.Fatalf("wrong length: %d", len(set.actors))
|
|
}
|
|
if _, ok := set.actors[&actor1]; ok {
|
|
test.Fatal("leaked item")
|
|
}
|
|
|
|
// find
|
|
found = set.find("type0")
|
|
test.Log("find:", found)
|
|
if found != &actor0 {
|
|
test.Fatalf("not equal: %p", found)
|
|
}
|
|
found = set.find("type1")
|
|
test.Log("find:", found)
|
|
if found != &actor2 {
|
|
test.Fatalf("not equal: %p", found)
|
|
}
|
|
|
|
// findAll
|
|
foundAll = set.findAll("type0")
|
|
test.Log("findAll:", foundAll)
|
|
if len(foundAll) != 1 {
|
|
test.Fatalf("wrong length: %d", len(foundAll))
|
|
}
|
|
if !mockInitializableIn(foundAll, &actor0) {
|
|
test.Fatal("missing item")
|
|
}
|
|
if mockInitializableIn(foundAll, &actor1) {
|
|
test.Fatal("leaked item")
|
|
}
|
|
foundAll = set.findAll("type1")
|
|
test.Log("findAll:", foundAll)
|
|
if len(foundAll) != 1 {
|
|
test.Fatalf("wrong length: %d", len(foundAll))
|
|
}
|
|
if !mockInitializableIn(foundAll, &actor2) {
|
|
test.Fatal("missing item")
|
|
}
|
|
|
|
// all
|
|
all = set.all()
|
|
test.Log("all:", all)
|
|
if len(all) != 2 {
|
|
test.Fatalf("wrong length: %d", len(all))
|
|
}
|
|
if !mockInitializableIn(all, &actor0) {
|
|
test.Fatal("missing item")
|
|
}
|
|
if mockInitializableIn(all, &actor1) {
|
|
test.Fatal("leaked item")
|
|
}
|
|
if !mockInitializableIn(all, &actor2) {
|
|
test.Fatal("missing item")
|
|
}
|
|
}
|
|
|
|
func TestActorSets(test *testing.T) {
|
|
sets := actorSets { }
|
|
|
|
actor0 := mockInitializable("type0")
|
|
actor1 := mockInitializable("type1")
|
|
sets.add(context.Background(), &actor0)
|
|
sets.add(context.Background(), &actor1)
|
|
test.Log("add")
|
|
|
|
if len(sets.inf) != 2 {
|
|
test.Fatalf("wrong length: %d", len(sets.inf))
|
|
}
|
|
if len(sets.all.actors) != 2 {
|
|
test.Fatalf("wrong length: %d", len(sets.all.actors))
|
|
}
|
|
if _, ok := sets.all.actors[&actor0]; !ok {
|
|
test.Fatal("missing item")
|
|
}
|
|
if _, ok := sets.all.actors[&actor1]; !ok {
|
|
test.Fatal("missing item")
|
|
}
|
|
if len(sets.initializable.actors) != 2 {
|
|
test.Fatalf("wrong length: %d", len(sets.initializable.actors))
|
|
}
|
|
if _, ok := sets.initializable.actors[&actor0]; !ok {
|
|
test.Fatal("missing item")
|
|
}
|
|
if _, ok := sets.initializable.actors[&actor1]; !ok {
|
|
test.Fatal("missing item")
|
|
}
|
|
if len(sets.configProcessor.actors) != 0 {
|
|
test.Fatalf("wrong length: %d", len(sets.configProcessor.actors))
|
|
}
|
|
if len(sets.configurable.actors) != 0 {
|
|
test.Fatalf("wrong length: %d", len(sets.configProcessor.actors))
|
|
}
|
|
if len(sets.runnable.actors) != 0 {
|
|
test.Fatalf("wrong length: %d", len(sets.runnable.actors))
|
|
}
|
|
if len(sets.trimmable.actors) != 0 {
|
|
test.Fatalf("wrong length: %d", len(sets.trimmable.actors))
|
|
}
|
|
|
|
info := sets.info(&actor0)
|
|
test.Log("info:", info)
|
|
if info.ctx == nil { test.Fatal("value is nil") }
|
|
if info.done == nil { test.Fatal("value is nil") }
|
|
if info.order != 1 { test.Fatal("not equal") }
|
|
|
|
info = sets.info(&actor1)
|
|
test.Log("info:", info)
|
|
if info.ctx == nil { test.Fatal("value is nil") }
|
|
if info.done == nil { test.Fatal("value is nil") }
|
|
if info.order != 2 { test.Fatal("not equal") }
|
|
|
|
sets.del(&actor0)
|
|
test.Log("del")
|
|
|
|
if len(sets.inf) != 1 {
|
|
test.Fatalf("wrong length: %d", len(sets.inf))
|
|
}
|
|
if len(sets.all.actors) != 1 {
|
|
test.Fatalf("wrong length: %d", len(sets.all.actors))
|
|
}
|
|
if _, ok := sets.all.actors[&actor1]; !ok {
|
|
test.Fatal("missing item")
|
|
}
|
|
if len(sets.initializable.actors) != 1 {
|
|
test.Fatalf("wrong length: %d", len(sets.initializable.actors))
|
|
}
|
|
if _, ok := sets.initializable.actors[&actor1]; !ok {
|
|
test.Fatal("missing item")
|
|
}
|
|
|
|
info = sets.info(&actor0)
|
|
test.Log("info:", info)
|
|
if info.ctx != nil { test.Fatal("value is non-nil") }
|
|
if info.done != nil { test.Fatal("value is non-nil") }
|
|
|
|
info = sets.info(&actor1)
|
|
test.Log("info:", info)
|
|
if info.ctx == nil { test.Fatal("value is nil") }
|
|
if info.done == nil { test.Fatal("value is nil") }
|
|
|
|
sets.del(&actor1)
|
|
test.Log("del")
|
|
|
|
if len(sets.configProcessor.actors) != 0 {
|
|
test.Fatalf("wrong length: %d", len(sets.configProcessor.actors))
|
|
}
|
|
if len(sets.configurable.actors) != 0 {
|
|
test.Fatalf("wrong length: %d", len(sets.configProcessor.actors))
|
|
}
|
|
if len(sets.initializable.actors) != 0 {
|
|
test.Fatalf("wrong length: %d", len(sets.configProcessor.actors))
|
|
}
|
|
if len(sets.runnable.actors) != 0 {
|
|
test.Fatalf("wrong length: %d", len(sets.runnable.actors))
|
|
}
|
|
if len(sets.trimmable.actors) != 0 {
|
|
test.Fatalf("wrong length: %d", len(sets.trimmable.actors))
|
|
}
|
|
}
|
|
|
|
func TestSortActors(test *testing.T) {
|
|
sets := actorSets { }
|
|
|
|
actor0 := mockInitializable("type0")
|
|
actor1 := mockInitializable("type1")
|
|
actor2 := mockInitializable("type2")
|
|
actor3 := mockInitializable("type3")
|
|
sets.add(context.Background(), &actor0)
|
|
sets.add(context.Background(), &actor1)
|
|
sets.add(context.Background(), &actor2)
|
|
sets.add(context.Background(), &actor3)
|
|
test.Log("add")
|
|
|
|
sorted := sortActors(&sets, []Actor { &actor1, &actor3, &actor2, &actor0 })
|
|
test.Log("sort:", sorted)
|
|
if len(sorted) != 4 { test.Fatalf("wrong length: %d", len(sorted)) }
|
|
if sorted[0] != &actor0 { test.Fatal("wrong position") }
|
|
if sorted[1] != &actor1 { test.Fatal("wrong position") }
|
|
if sorted[2] != &actor2 { test.Fatal("wrong position") }
|
|
if sorted[3] != &actor3 { test.Fatal("wrong position") }
|
|
}
|
|
|
|
func mockInitializableIn(haystack []Initializable, needle *mockInitializable) bool {
|
|
for _, initializable := range haystack {
|
|
if initializable == needle { return true }
|
|
}
|
|
return false
|
|
}
|