Skip to content

Commit

Permalink
Fix spawning actors with duplicate id bug (#164)
Browse files Browse the repository at this point in the history
* fixed duplicate id bug

* response holds error and returns it properly

* reverted changes to response

* trigger-build

* moved proc.Start() to registry.add()

* improved unit test
  • Loading branch information
troygilman0 authored Aug 22, 2024
1 parent 0921477 commit d199384
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 0 additions & 1 deletion actor/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func (e *Engine) SpawnFunc(f func(*Context), kind string, opts ...OptFunc) *PID
// with custom created Processes. Take a look at the streamWriter as an example.
func (e *Engine) SpawnProc(p Processer) *PID {
e.Registry.add(p)
p.Start()
return p.PID()
}

Expand Down
17 changes: 16 additions & 1 deletion actor/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestSpawn(t *testing.T) {
wg.Wait()
}

func TestSpawnDuplicateId(t *testing.T) {
func TestSpawnDuplicateKind(t *testing.T) {
e, err := NewEngine(NewEngineConfig())
require.NoError(t, err)
wg := sync.WaitGroup{}
Expand All @@ -234,6 +234,21 @@ func TestSpawnDuplicateId(t *testing.T) {
wg.Wait()
}

func TestSpawnDuplicateId(t *testing.T) {
e, err := NewEngine(NewEngineConfig())
require.NoError(t, err)
var startsCount int32 = 0
receiveFunc := func(t *testing.T, ctx *Context) {
switch ctx.Message().(type) {
case Initialized:
atomic.AddInt32(&startsCount, 1)
}
}
e.Spawn(NewTestProducer(t, receiveFunc), "dummy", WithID("1"))
e.Spawn(NewTestProducer(t, receiveFunc), "dummy", WithID("1"))
assert.Equal(t, int32(1), startsCount) // should only spawn one actor
}

func TestStopWaitGroup(t *testing.T) {
var (
wg = sync.WaitGroup{}
Expand Down
1 change: 1 addition & 0 deletions actor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ func (r *Registry) add(proc Processer) {
}
r.lookup[id] = proc
r.mu.Unlock()
proc.Start()
}

0 comments on commit d199384

Please sign in to comment.