Skip to content

Commit e1f7444

Browse files
committed
fix: Fixed tests
1 parent 0a324d5 commit e1f7444

File tree

10 files changed

+217
-609
lines changed

10 files changed

+217
-609
lines changed

internal/actor/actor_comprehensive_test.go

Lines changed: 68 additions & 370 deletions
Large diffs are not rendered by default.

internal/actor/actor_test.go

Lines changed: 10 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2,120 +2,11 @@ package actor
22

33
import (
44
"context"
5-
"errors"
65
"sync"
7-
"sync/atomic"
86
"testing"
97
"time"
108
)
119

12-
// TestMessage is a simple test message type
13-
type TestMessage struct {
14-
ID string
15-
Content string
16-
}
17-
18-
func (m *TestMessage) Type() string {
19-
return "test"
20-
}
21-
22-
// CounterMessage is used for counting operations
23-
type CounterMessage struct{}
24-
25-
func (m *CounterMessage) Type() string {
26-
return "counter"
27-
}
28-
29-
// ErrorMessage is used to trigger errors
30-
type ErrorMessage struct{}
31-
32-
func (m *ErrorMessage) Type() string {
33-
return "error"
34-
}
35-
36-
// TestActor is a simple actor implementation for testing
37-
type TestActor struct {
38-
id string
39-
receivedMsgs []Message
40-
receiveCount atomic.Int32
41-
startCalled atomic.Bool
42-
stopCalled atomic.Bool
43-
shouldError atomic.Bool
44-
mu sync.Mutex
45-
receiveHandler func(ctx context.Context, msg Message) error
46-
}
47-
48-
func NewTestActor(id string) *TestActor {
49-
return &TestActor{
50-
id: id,
51-
receivedMsgs: make([]Message, 0),
52-
}
53-
}
54-
55-
func (a *TestActor) ID() string {
56-
return a.id
57-
}
58-
59-
func (a *TestActor) Start(ctx context.Context) error {
60-
a.startCalled.Store(true)
61-
return nil
62-
}
63-
64-
func (a *TestActor) Stop(ctx context.Context) error {
65-
a.stopCalled.Store(true)
66-
return nil
67-
}
68-
69-
func (a *TestActor) Receive(ctx context.Context, msg Message) error {
70-
a.receiveCount.Add(1)
71-
72-
if a.receiveHandler != nil {
73-
return a.receiveHandler(ctx, msg)
74-
}
75-
76-
a.mu.Lock()
77-
a.receivedMsgs = append(a.receivedMsgs, msg)
78-
a.mu.Unlock()
79-
80-
if a.shouldError.Load() {
81-
return errors.New("test error")
82-
}
83-
84-
if _, ok := msg.(*ErrorMessage); ok {
85-
return errors.New("error message received")
86-
}
87-
88-
return nil
89-
}
90-
91-
func (a *TestActor) GetReceivedMessages() []Message {
92-
a.mu.Lock()
93-
defer a.mu.Unlock()
94-
msgs := make([]Message, len(a.receivedMsgs))
95-
copy(msgs, a.receivedMsgs)
96-
return msgs
97-
}
98-
99-
func (a *TestActor) GetReceiveCount() int32 {
100-
return a.receiveCount.Load()
101-
}
102-
103-
func (a *TestActor) WasStartCalled() bool {
104-
return a.startCalled.Load()
105-
}
106-
107-
func (a *TestActor) WasStopCalled() bool {
108-
return a.stopCalled.Load()
109-
}
110-
111-
func (a *TestActor) SetShouldError(val bool) {
112-
a.shouldError.Store(val)
113-
}
114-
115-
func (a *TestActor) SetReceiveHandler(handler func(ctx context.Context, msg Message) error) {
116-
a.receiveHandler = handler
117-
}
118-
11910
// TestNewActorRef tests creating a new actor reference
12011
func TestNewActorRef(t *testing.T) {
12112
actor := NewTestActor("test-1")
@@ -335,7 +226,7 @@ func TestActorRefReceiveError(t *testing.T) {
335226
}
336227
}
337228

338-
func TestActorRefSequentialProcessing(t *testing.T) {
229+
func TestActorRefSequentialProcessing_Variants(t *testing.T) {
339230
ctx := context.Background()
340231
actor := NewTestActor("test-1")
341232
ref := NewActorRef("test-1", actor, 10, WithSequentialProcessing())
@@ -367,7 +258,7 @@ func TestActorRefSequentialProcessing(t *testing.T) {
367258
}
368259

369260
// TestActorRefContextCancellation tests context cancellation during message processing
370-
func TestActorRefContextCancellation(t *testing.T) {
261+
func TestActorRefContextCancellation_Basic(t *testing.T) {
371262
ctx, cancel := context.WithCancel(context.Background())
372263
actor := NewTestActor("test-1")
373264

@@ -414,7 +305,7 @@ func TestActorRefContextCancellation(t *testing.T) {
414305
}
415306

416307
// TestSystemSpawn tests spawning actors in the system
417-
func TestSystemSpawn(t *testing.T) {
308+
func TestSystemSpawn_Basic(t *testing.T) {
418309
ctx := context.Background()
419310
system := NewSystem()
420311

@@ -446,7 +337,7 @@ func TestSystemSpawn(t *testing.T) {
446337
}
447338

448339
// TestSystemGet tests retrieving actors from the system
449-
func TestSystemGet(t *testing.T) {
340+
func TestSystemGet_Basic(t *testing.T) {
450341
ctx := context.Background()
451342
system := NewSystem()
452343

@@ -479,7 +370,7 @@ func TestSystemGet(t *testing.T) {
479370
}
480371

481372
// TestSystemStop tests stopping individual actors
482-
func TestSystemStop(t *testing.T) {
373+
func TestSystemStop_Basic(t *testing.T) {
483374
ctx := context.Background()
484375
system := NewSystem()
485376

@@ -516,7 +407,7 @@ func TestSystemStop(t *testing.T) {
516407
}
517408

518409
// TestSystemStopAll tests stopping all actors
519-
func TestSystemStopAll(t *testing.T) {
410+
func TestSystemStopAll_Basic(t *testing.T) {
520411
ctx := context.Background()
521412
system := NewSystem()
522413

@@ -560,7 +451,7 @@ func TestSystemStopAll(t *testing.T) {
560451
}
561452

562453
// TestSystemConcurrentAccess tests concurrent access to the system
563-
func TestSystemConcurrentAccess(t *testing.T) {
454+
func TestSystemConcurrentAccess_Basic(t *testing.T) {
564455
ctx := context.Background()
565456
system := NewSystem()
566457

@@ -597,7 +488,7 @@ func TestSystemConcurrentAccess(t *testing.T) {
597488
}
598489

599490
// TestActorCommunication tests communication between actors
600-
func TestActorCommunication(t *testing.T) {
491+
func TestActorCommunication_Basic(t *testing.T) {
601492
ctx := context.Background()
602493
system := NewSystem()
603494

@@ -648,7 +539,7 @@ func TestActorCommunication(t *testing.T) {
648539
}
649540

650541
// BenchmarkActorSend benchmarks sending messages to an actor
651-
func BenchmarkActorSend(b *testing.B) {
542+
func BenchmarkActorSend_Basic(b *testing.B) {
652543
ctx := context.Background()
653544
actor := NewTestActor("bench-actor")
654545
ref := NewActorRef("bench-actor", actor, 10000)
@@ -676,8 +567,7 @@ func BenchmarkActorSend(b *testing.B) {
676567
}
677568
}
678569

679-
// BenchmarkActorSpawn benchmarks spawning actors
680-
func BenchmarkActorSpawn(b *testing.B) {
570+
func BenchmarkActorSpawn_Basic(b *testing.B) {
681571
ctx := context.Background()
682572

683573
b.ResetTimer()

internal/actor/session_storage_actor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewSessionStorageActor(name string) (*SessionStorageActor, error) {
2424
func NewSessionStorageActorWithConfig(name string, configFunc func() *config.AutoSaveConfig) (*SessionStorageActor, error) {
2525
var storage *session.SessionStorage
2626
var err error
27-
27+
2828
if configFunc != nil {
2929
storage, err = session.NewSessionStorageWithConfig(configFunc)
3030
} else {

internal/actor/test_utils.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package actor
2+
3+
import (
4+
"context"
5+
"errors"
6+
"sync"
7+
"sync/atomic"
8+
)
9+
10+
// TestMessage is a simple test message type
11+
type TestMessage struct {
12+
ID string
13+
Content string
14+
}
15+
16+
func (m *TestMessage) Type() string {
17+
return "test"
18+
}
19+
20+
// CounterMessage is used for counting operations
21+
type CounterMessage struct{}
22+
23+
func (m *CounterMessage) Type() string {
24+
return "counter"
25+
}
26+
27+
// ErrorMessage is used to trigger errors
28+
type ErrorMessage struct{}
29+
30+
func (m *ErrorMessage) Type() string {
31+
return "error"
32+
}
33+
34+
// TestActor is a simple actor implementation for testing
35+
type TestActor struct {
36+
id string
37+
receivedMsgs []Message
38+
receiveCount atomic.Int32
39+
startCalled atomic.Bool
40+
stopCalled atomic.Bool
41+
shouldError atomic.Bool
42+
mu sync.Mutex
43+
receiveHandler func(ctx context.Context, msg Message) error
44+
}
45+
46+
func NewTestActor(id string) *TestActor {
47+
return &TestActor{
48+
id: id,
49+
receivedMsgs: make([]Message, 0),
50+
}
51+
}
52+
53+
func (a *TestActor) ID() string {
54+
return a.id
55+
}
56+
57+
func (a *TestActor) Start(ctx context.Context) error {
58+
a.startCalled.Store(true)
59+
return nil
60+
}
61+
62+
func (a *TestActor) Stop(ctx context.Context) error {
63+
a.stopCalled.Store(true)
64+
return nil
65+
}
66+
67+
func (a *TestActor) Receive(ctx context.Context, msg Message) error {
68+
a.receiveCount.Add(1)
69+
70+
if a.receiveHandler != nil {
71+
return a.receiveHandler(ctx, msg)
72+
}
73+
74+
a.mu.Lock()
75+
a.receivedMsgs = append(a.receivedMsgs, msg)
76+
a.mu.Unlock()
77+
78+
if a.shouldError.Load() {
79+
return errors.New("test error")
80+
}
81+
82+
if _, ok := msg.(*ErrorMessage); ok {
83+
return errors.New("error message received")
84+
}
85+
86+
return nil
87+
}
88+
89+
func (a *TestActor) GetReceivedMessages() []Message {
90+
a.mu.Lock()
91+
defer a.mu.Unlock()
92+
msgs := make([]Message, len(a.receivedMsgs))
93+
copy(msgs, a.receivedMsgs)
94+
return msgs
95+
}
96+
97+
func (a *TestActor) GetReceiveCount() int32 {
98+
return a.receiveCount.Load()
99+
}
100+
101+
func (a *TestActor) WasStartCalled() bool {
102+
return a.startCalled.Load()
103+
}
104+
105+
func (a *TestActor) WasStopCalled() bool {
106+
return a.stopCalled.Load()
107+
}
108+
109+
func (a *TestActor) SetShouldError(val bool) {
110+
a.shouldError.Store(val)
111+
}
112+
113+
func (a *TestActor) SetReceiveHandler(handler func(ctx context.Context, msg Message) error) {
114+
a.receiveHandler = handler
115+
}

0 commit comments

Comments
 (0)