➜ go test ./... -v
=== RUN TestListen
--- PASS: TestListen (0.00s)
=== RUN TestMultipleEvents
--- PASS: TestMultipleEvents (0.00s)
=== RUN TestMultipleListenersToSameEvent
--- PASS: TestMultipleListenersToSameEvent (0.00s)
=== RUN TestEmitWithData
--- PASS: TestEmitWithData (0.00s)
=== RUN TestRemoveEvent
--- PASS: TestRemoveEvent (0.00s)
=== RUN TestKeepRunningOnPanic
Recovered in f ahhh
--- PASS: TestKeepRunningOnPanic (0.00s)
=== RUN TestListenOnce
--- PASS: TestListenOnce (0.00s)
=== RUN TestReset
--- PASS: TestReset (0.00s)
PASS
ok github.com/afyadigital/eventemitter 0.003s
Created
May 29, 2023 19:13
-
-
Save thiagozs/4fc1074b1a0d78f4bbfb4b20f6668c82 to your computer and use it in GitHub Desktop.
Test case eventemitter golang chalenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package eventemitter | |
import ( | |
"fmt" | |
) | |
type Data func(data interface{}) | |
var ( | |
datas = make(map[string][]Data) | |
lo = make(map[string]Data) | |
) | |
func Reset() { | |
datas = make(map[string][]Data) | |
lo = make(map[string]Data) | |
} | |
func Listen(topic string, fn Data) { | |
datas[topic] = append(datas[topic], fn) | |
} | |
func Emit(topic string, in interface{}) { | |
if d, ok := lo[topic]; ok { | |
callEvent(d, in) | |
delete(lo, topic) | |
return | |
} | |
vs, ok := datas[topic] | |
if !ok { | |
return | |
} | |
for _, v := range vs { | |
callEvent(v, in) | |
} | |
} | |
func callEvent(data Data, in interface{}) { | |
defer func() { | |
if r := recover(); r != nil { | |
fmt.Println("Recovered in f", r) | |
} | |
}() | |
data(in) | |
} | |
func RemoveEvent(topic string) { | |
delete(datas, topic) | |
} | |
func ListenOnce(topic string, fn Data) { | |
lo[topic] = fn | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package eventemitter_test | |
import ( | |
"testing" | |
"github.com/afyadigital/eventemitter" | |
"github.com/stretchr/testify/require" | |
) | |
func TestListen(t *testing.T) { | |
t.Cleanup(eventemitter.Reset) | |
eventName := "TestListen" | |
actionCalled := false | |
action := func(data interface{}) { | |
actionCalled = true | |
} | |
eventemitter.Listen(eventName, action) | |
require.False(t, actionCalled) | |
eventemitter.Emit("TestListen", nil) | |
require.True(t, actionCalled) | |
} | |
func TestMultipleEvents(t *testing.T) { | |
t.Cleanup(eventemitter.Reset) | |
eventNameA := "TestMultipleEventsA" | |
actionCalled1 := false | |
action1 := func(data interface{}) { | |
actionCalled1 = true | |
} | |
eventNameB := "TestMultipleEventsB" | |
actionCalled2 := false | |
action2 := func(data interface{}) { | |
actionCalled2 = true | |
} | |
eventemitter.Listen(eventNameA, action1) | |
eventemitter.Listen(eventNameB, action2) | |
require.False(t, actionCalled1) | |
require.False(t, actionCalled2) | |
eventemitter.Emit("TestMultipleEventsA", nil) | |
require.True(t, actionCalled1) | |
require.False(t, actionCalled2) | |
eventemitter.Emit("TestMultipleEventsB", nil) | |
require.True(t, actionCalled1) | |
require.True(t, actionCalled2) | |
} | |
func TestMultipleListenersToSameEvent(t *testing.T) { | |
t.Cleanup(eventemitter.Reset) | |
eventNameA := "TestMultipleListenersToSameEvent" | |
actionCalled1 := false | |
action1 := func(data interface{}) { | |
actionCalled1 = true | |
} | |
actionCalled2 := false | |
action2 := func(data interface{}) { | |
actionCalled2 = true | |
} | |
eventemitter.Listen(eventNameA, action1) | |
eventemitter.Listen(eventNameA, action2) | |
require.False(t, actionCalled1) | |
require.False(t, actionCalled2) | |
eventemitter.Emit("TestMultipleListenersToSameEvent", nil) | |
require.True(t, actionCalled1) | |
require.True(t, actionCalled2) | |
} | |
func TestEmitWithData(t *testing.T) { | |
t.Cleanup(eventemitter.Reset) | |
eventName := "TestEmitWithData" | |
actionCalled := false | |
var actionData interface{} | |
action := func(data interface{}) { | |
actionCalled = true | |
actionData = data | |
} | |
eventemitter.Listen(eventName, action) | |
require.False(t, actionCalled) | |
eventemitter.Emit("TestEmitWithData", "oi") | |
require.True(t, actionCalled) | |
require.Equal(t, "oi", actionData) | |
} | |
func TestRemoveEvent(t *testing.T) { | |
t.Cleanup(eventemitter.Reset) | |
eventName := "TestRemoveEvent" | |
actionCalled := false | |
action := func(data interface{}) { | |
actionCalled = true | |
} | |
eventemitter.Listen(eventName, action) | |
eventemitter.RemoveEvent(eventName) | |
require.False(t, actionCalled) | |
eventemitter.Emit("TestRemoveEvent", nil) | |
require.False(t, actionCalled) | |
} | |
func TestKeepRunningOnPanic(t *testing.T) { | |
t.Cleanup(eventemitter.Reset) | |
eventName := "TestKeepRunningOnPanic" | |
action1Called := false | |
action1 := func(data interface{}) { | |
action1Called = true | |
} | |
actionPanic := func(data interface{}) { | |
panic("ahhh") | |
} | |
action2Called := false | |
action2 := func(data interface{}) { | |
action2Called = true | |
} | |
eventemitter.Listen(eventName, action1) | |
eventemitter.Listen(eventName, actionPanic) | |
eventemitter.Listen(eventName, action2) | |
require.NotPanics(t, func() { | |
eventemitter.Emit("TestKeepRunningOnPanic", nil) | |
}) | |
require.True(t, action1Called) | |
require.True(t, action2Called) | |
} | |
func TestListenOnce(t *testing.T) { | |
t.Cleanup(eventemitter.Reset) | |
eventName := "TestListenOnce" | |
actionCalledTimes := 0 | |
action := func(data interface{}) { | |
actionCalledTimes++ | |
} | |
eventemitter.ListenOnce(eventName, action) | |
require.Equal(t, 0, actionCalledTimes) | |
eventemitter.Emit("TestListenOnce", nil) | |
require.Equal(t, 1, actionCalledTimes) | |
eventemitter.Emit("TestListenOnce", nil) | |
require.Equal(t, 1, actionCalledTimes) | |
} | |
func TestReset(t *testing.T) { | |
t.Cleanup(eventemitter.Reset) | |
eventName1 := "TestReset1" | |
action1Called := false | |
action1 := func(data interface{}) { | |
action1Called = true | |
} | |
eventName2 := "TestReset2" | |
action2Called := false | |
action2 := func(data interface{}) { | |
action2Called = true | |
} | |
eventemitter.Listen(eventName1, action1) | |
eventemitter.Listen(eventName2, action2) | |
eventemitter.Reset() | |
eventemitter.Emit("TestReset1", nil) | |
eventemitter.Emit("TestReset2", nil) | |
require.False(t, action1Called) | |
require.False(t, action2Called) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module github.com/afyadigital/eventemitter | |
go 1.16 | |
require github.com/stretchr/testify v1.7.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | |
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | |
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | |
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | |
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | |
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | |
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | |
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | |
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | |
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | |
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment