wip
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"reflect"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
cart_messages "git.k6n.net/go-cart-actor/proto/cart"
|
||||
)
|
||||
@@ -104,31 +105,103 @@ func TestRegisteredMutationBasics(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// func TestConcurrentSafeRegistrationLookup(t *testing.T) {
|
||||
// // This test is light-weight; it ensures locks don't deadlock under simple concurrent access.
|
||||
// reg := NewMutationRegistry().(*ProtoMutationRegistry)
|
||||
// mut := NewMutation[cartState, *messages.Noop](
|
||||
// func(state *cartState, msg *messages.Noop) error { state.calls++; return nil },
|
||||
// func() *messages.Noop { return &messages.Noop{} },
|
||||
// )
|
||||
// reg.RegisterMutations(mut)
|
||||
func TestEventChannel(t *testing.T) {
|
||||
reg := NewMutationRegistry().(*ProtoMutationRegistry)
|
||||
|
||||
// done := make(chan struct{})
|
||||
// const workers = 25
|
||||
// for i := 0; i < workers; i++ {
|
||||
// go func() {
|
||||
// for j := 0; j < 100; j++ {
|
||||
// _, _ = reg.Create("Noop")
|
||||
// _, _ = reg.GetTypeName(&messages.Noop{})
|
||||
// _ = reg.Apply(&cartState{}, &messages.Noop{})
|
||||
// }
|
||||
// done <- struct{}{}
|
||||
// }()
|
||||
// }
|
||||
addItemMutation := NewMutation(
|
||||
func(state *cartState, msg *cart_messages.AddItem) error {
|
||||
state.calls++
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
// for i := 0; i < workers; i++ {
|
||||
// <-done
|
||||
// }
|
||||
// }
|
||||
reg.RegisterMutations(addItemMutation)
|
||||
|
||||
eventCh := make(chan ApplyResult, 10)
|
||||
reg.SetEventChannel(eventCh)
|
||||
|
||||
state := &cartState{}
|
||||
add := &cart_messages.AddItem{ItemId: 42, Quantity: 3, Sku: "ABC"}
|
||||
results, err := reg.Apply(context.Background(), state, add)
|
||||
if err != nil {
|
||||
t.Fatalf("Apply returned error: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
|
||||
// Receive from channel with timeout
|
||||
select {
|
||||
case res := <-eventCh:
|
||||
if res.Type != "AddItem" {
|
||||
t.Fatalf("expected type AddItem, got %s", res.Type)
|
||||
}
|
||||
if res.Error != nil {
|
||||
t.Fatalf("expected no error, got %v", res.Error)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatalf("expected to receive event on channel within timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventChannelClosed(t *testing.T) {
|
||||
reg := NewMutationRegistry().(*ProtoMutationRegistry)
|
||||
|
||||
addItemMutation := NewMutation(
|
||||
func(state *cartState, msg *cart_messages.AddItem) error {
|
||||
state.calls++
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
reg.RegisterMutations(addItemMutation)
|
||||
|
||||
eventCh := make(chan ApplyResult, 10)
|
||||
reg.SetEventChannel(eventCh)
|
||||
|
||||
close(eventCh) // Close the channel to simulate external close
|
||||
|
||||
state := &cartState{}
|
||||
add := &cart_messages.AddItem{ItemId: 42, Quantity: 3, Sku: "ABC"}
|
||||
// This should not panic due to recover in goroutine
|
||||
results, err := reg.Apply(context.Background(), state, add)
|
||||
if err != nil {
|
||||
t.Fatalf("Apply returned error: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
func TestEventChannelUnbufferedNoListener(t *testing.T) {
|
||||
reg := NewMutationRegistry().(*ProtoMutationRegistry)
|
||||
|
||||
addItemMutation := NewMutation(
|
||||
func(state *cartState, msg *cart_messages.AddItem) error {
|
||||
state.calls++
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
reg.RegisterMutations(addItemMutation)
|
||||
|
||||
eventCh := make(chan ApplyResult) // unbuffered
|
||||
reg.SetEventChannel(eventCh)
|
||||
|
||||
// No goroutine reading from eventCh
|
||||
|
||||
state := &cartState{}
|
||||
add := &cart_messages.AddItem{ItemId: 42, Quantity: 3, Sku: "ABC"}
|
||||
results, err := reg.Apply(context.Background(), state, add)
|
||||
if err != nil {
|
||||
t.Fatalf("Apply returned error: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
// Since no listener, the send should go to default and not block
|
||||
// Test passes if Apply completes without hanging
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
Reference in New Issue
Block a user