Skip to content

Commit 905657a

Browse files
committed
add more tests
1 parent bfce767 commit 905657a

9 files changed

+159
-12
lines changed

manager-raise.go renamed to manager-emit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package events
22

3-
// Raise - informs all subscribers about the event
4-
func (thisRef *Manager) Raise(eventName string) {
3+
// Emit - informs all subscribers about the event
4+
func (thisRef *Manager) Emit(eventName string) {
55
thisRef.eventsMutex.RLock()
66
defer thisRef.eventsMutex.RUnlock()
77

@@ -13,8 +13,8 @@ func (thisRef *Manager) Raise(eventName string) {
1313
thisRef.removeAllCallOnce(eventName)
1414
}
1515

16-
// RaiseWithData - informs all subscribers about the event with data
17-
func (thisRef *Manager) RaiseWithData(eventName string, data []byte) {
16+
// EmitWithData - informs all subscribers about the event with data
17+
func (thisRef *Manager) EmitWithData(eventName string, data []byte) {
1818
thisRef.eventsMutex.RLock()
1919
defer thisRef.eventsMutex.RUnlock()
2020

readme.md

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,127 @@
11
# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Events
2-
[![GoDoc](https://godoc.org/github.com/codemodify/systemkit-logging?status.svg)](https://godoc.org/github.com/codemodify/systemkit-logging)
2+
[![GoDoc](https://godoc.org/github.com/codemodify/systemkit-logging?status.svg)](https://godoc.org/github.com/codemodify/systemkit-events)
33
[![0-License](https://img.shields.io/badge/license-0--license-brightgreen)](https://github.com/codemodify/TheFreeLicense)
44
[![Go Report Card](https://goreportcard.com/badge/github.com/codemodify/systemkit-logging)](https://goreportcard.com/report/github.com/codemodify/systemkit-logging)
55
[![Test Status](https://github.com/danawoodman/systemservice/workflows/Test/badge.svg)](https://github.com/danawoodman/systemservice/actions)
66
![code size](https://img.shields.io/github/languages/code-size/codemodify/SystemKit?style=flat-square)
77

8-
A Swiss-Knife collection of helpers for all cases of coding life
8+
#### Robust events for Go. It takes `0.1` seconds to send/receive 1 Million events.
9+
10+
11+
# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Install
12+
```go
13+
go get github.com/codemodify/systemkit-events
14+
```
15+
16+
# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) API
17+
18+
  |  
19+
--- | ---
20+
On(`event`, `handler`, `callOnce`) | Subscribe
21+
OnWithData(`event`, `handler`, `callOnce`) | Subscribe with payload
22+
Off(`event`, `handlerRef`) | Unsubscribe
23+
OffWithData(`event`, `handlerRef`) | Unsubscribe with payload
24+
Emit(`event`) | Emit
25+
EmitWithData(`event`, `data`) | Emit with payload
26+
27+
28+
29+
# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Usage: Subscribe + Notify
30+
```go
31+
package main
32+
33+
import (
34+
events "github.com/codemodify/systemkit-events"
35+
)
36+
37+
func main() {
38+
events.Events().On("PING", func() {
39+
// FIMXE: will be called
40+
})
41+
42+
events.Events().Emit("PING")
43+
}
44+
```
45+
46+
# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Usage: Subscribe (data) + Notify (data)
47+
```go
48+
package main
49+
50+
import (
51+
events "github.com/codemodify/systemkit-events"
52+
)
53+
54+
func main() {
55+
events.Events().OnWithData("PING", func(data []byte) {
56+
// FIMXE: will be called
57+
})
58+
59+
events.Events().EmitWithData("PING", []byte("PING-DATA"))
60+
}
61+
```
62+
63+
# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Usage: Subscribe (yes-no-data) + Notify (data)
64+
```go
65+
package main
66+
67+
import (
68+
events "github.com/codemodify/systemkit-events"
69+
)
70+
71+
func main() {
72+
events.Events().OnWithData("PING", func(data []byte) {
73+
// FIMXE: will be called
74+
})
75+
76+
events.Events().On("PING", func() {
77+
// FIMXE: will be called
78+
})
79+
80+
events.Events().EmitWithData("PING", []byte("PING-DATA"))
81+
}
82+
```
83+
84+
# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Usage: Subscribe (yes-no-data) + Notify
85+
```go
86+
package main
87+
88+
import (
89+
events "github.com/codemodify/systemkit-events"
90+
)
91+
92+
func main() {
93+
events.Events().OnWithData("PING", func(data []byte) {
94+
// FIMXE: will be called, data will be nil
95+
})
96+
97+
events.Events().On("PING", func() {
98+
// FIMXE: will be called
99+
})
100+
101+
events.Events().Emit("PING")
102+
}
103+
```
104+
105+
# ![](https://fonts.gstatic.com/s/i/materialicons/bookmarks/v4/24px.svg) Usage: Subscribe Once + Notify
106+
```go
107+
package main
108+
109+
import (
110+
events "github.com/codemodify/systemkit-events"
111+
)
112+
113+
func main() {
114+
115+
handler1CallOnce := true
116+
events.Events().On("PING", func() {
117+
// FIMXE: will be called ONCE
118+
}, handler1CallOnce)
119+
120+
handler3CallOnce := false
121+
events.Events().On("PING", func() {
122+
// FIMXE: will be called UNTIL "events.Events().Off()" is called
123+
}, handler3CallOnce)
124+
125+
events.Events().Emit("PING")
126+
}
127+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
6+
events "github.com/codemodify/systemkit-events"
7+
)
8+
9+
func Benchmark_benchmark_one_mil(b *testing.B) {
10+
11+
const pingEvent = "BENCH"
12+
const pingData = "PING-DATA"
13+
14+
events.Events().OnWithData(pingEvent, func(data []byte) {
15+
// DO SOMETHING WITH DATA
16+
})
17+
18+
b.ResetTimer()
19+
b.StartTimer()
20+
count := 0
21+
countMax := 1000000
22+
for ; count < countMax; count++ {
23+
events.Events().EmitWithData(pingEvent, []byte(pingData))
24+
}
25+
b.StopTimer()
26+
27+
b.Logf("SEND/RECV: %d", count)
28+
}

tests/01-raise-nodata-on-nodata_test.go renamed to tests/unit/01-raise-nodata-on-nodata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Test_RaiseNoData_OnNoData(t *testing.T) {
1616
go func() {
1717
for {
1818
time.Sleep(1 * time.Second)
19-
events.Events().Raise(pingEvent)
19+
events.Events().Emit(pingEvent)
2020
}
2121
}()
2222

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Test_RaiseData_OnData(t *testing.T) {
1717
go func() {
1818
for {
1919
time.Sleep(1 * time.Second)
20-
events.Events().RaiseWithData(pingEvent, []byte(pingData))
20+
events.Events().EmitWithData(pingEvent, []byte(pingData))
2121
}
2222
}()
2323

tests/03-raise-data-on-nodata_test.go renamed to tests/unit/03-raise-data-on-nodata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Test_RaiseData_OnNoData(t *testing.T) {
1717
go func() {
1818
for {
1919
time.Sleep(1 * time.Second)
20-
events.Events().RaiseWithData(pingEvent, []byte(pingData))
20+
events.Events().EmitWithData(pingEvent, []byte(pingData))
2121
}
2222
}()
2323

tests/04-raise-data-on-data-on-nodata_test.go renamed to tests/unit/04-raise-data-on-data-on-nodata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Test_RaiseData_OnData_OnNoData(t *testing.T) {
1717
go func() {
1818
for {
1919
time.Sleep(1 * time.Second)
20-
events.Events().RaiseWithData(pingEvent, []byte(pingData))
20+
events.Events().EmitWithData(pingEvent, []byte(pingData))
2121
}
2222
}()
2323

tests/05-raise-nodata-on-data-on-nodata_test.go renamed to tests/unit/05-raise-nodata-on-data-on-nodata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Test_RaiseNoData_OnData_OnNoData(t *testing.T) {
1616
go func() {
1717
for {
1818
time.Sleep(1 * time.Second)
19-
events.Events().Raise(pingEvent)
19+
events.Events().Emit(pingEvent)
2020
}
2121
}()
2222

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Test_Raise_On_Once(t *testing.T) {
1616
go func() {
1717
for {
1818
time.Sleep(1 * time.Second)
19-
events.Events().Raise(pingEvent)
19+
events.Events().Emit(pingEvent)
2020
}
2121
}()
2222

0 commit comments

Comments
 (0)