Skip to content

Commit

Permalink
👔 up: set EnableLock default is False
Browse files Browse the repository at this point in the history
- add some new issues test cases
- replace pkg stretchr/testify to gookit/goutil
- add code lint check action script
  • Loading branch information
inhere committed Feb 5, 2024
1 parent 51019c4 commit b1597a2
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go_version: [1.19, 1.18, '1.20']
go_version: [1.19, 1.21, '1.20']
os: [ubuntu-latest, macOS-latest]

steps:
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CodeLinter
on:
pull_request:
paths:
- 'go.mod'
- '**.go'
- '**.yml'
push:
paths:
- '**.go'
- 'go.mod'
- '**.yml'

jobs:

test:
name: Code linter
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Setup Go Faster
uses: WillAbides/setup-go-faster@v1.13.0
timeout-minutes: 3
with:
go-version: "*"

- name: Revive lint check
uses: docker://morphy/revive-action:v2
with:
# Exclude patterns, separated by semicolons (optional)
exclude: "./_examples/...;./testdata/..."

- name: Run static check
uses: reviewdog/action-staticcheck@v1
if: ${{ github.event_name == 'pull_request'}}
with:
github_token: ${{ secrets.github_token }}
# Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review].
reporter: github-pr-check
# Report all results. [added,diff_context,file,nofilter].
filter_mode: added
# Exit with 1 when it find at least one finding.
fail_on_error: true
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type ManagerFace interface {

// Options event manager config options
type Options struct {
// EnableLock enable lock on fire event. default is TRUE.
// EnableLock enable lock on fire event. default is False.
EnableLock bool
// ChannelSize for fire events by goroutine
ChannelSize int
Expand Down
2 changes: 1 addition & 1 deletion event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/gookit/event"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

type testListener struct {
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/gookit/event

go 1.18
go 1.19

require github.com/stretchr/testify v1.8.4
require github.com/gookit/goutil v0.6.15

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
17 changes: 11 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/gookit/goutil v0.6.15 h1:mMQ0ElojNZoyPD0eVROk5QXJPh2uKR4g06slgPDF5Jo=
github.com/gookit/goutil v0.6.15/go.mod h1:qdKdYEHQdEtyH+4fNdQNZfJHhI0jUZzHxQVAV3DaMDY=
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/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
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=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
29 changes: 28 additions & 1 deletion issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"bytes"
"fmt"
"testing"
"time"

"github.com/gookit/event"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

type testNotify struct{}
Expand Down Expand Up @@ -86,3 +87,29 @@ func TestIssues_20(t *testing.T) {

// dump.P(buf.String())
}

// https://github.com/gookit/event/issues/61
// prepare: 此时设置 ConsumerNum = 10, 每个任务耗时1s, 触发100个任务
// expected: 10s左右执行完所有任务
// actual: 执行了 100s左右
func TestIssues_61(t *testing.T) {
var em = event.NewManager("default", func(o *event.Options) {
o.ConsumerNum = 10
o.EnableLock = false
})
defer em.CloseWait()

var listener event.ListenerFunc = func(e event.Event) error {
time.Sleep(1 * time.Second)
fmt.Println("event received!", e.Name(), "index", e.Get("arg0"))
return nil
}

em.On("app.evt1", listener, event.Normal)

for i := 0; i < 20; i++ {
em.FireAsync(event.New("app.evt1", event.M{"arg0": i}))
}

fmt.Println("publish event finished!")
}
2 changes: 1 addition & 1 deletion listener_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package event
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

type globalTestVal struct {
Expand Down
18 changes: 10 additions & 8 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewManager(name string, fns ...OptionFn) *Manager {
listenedNames: make(map[string]int),
}

em.EnableLock = true
// em.EnableLock = true
// for async fire by goroutine
em.ConsumerNum = defaultConsumerNum
em.ChannelSize = defaultChannelSize
Expand Down Expand Up @@ -325,13 +325,6 @@ func (em *Manager) firePathMode(name string, e Event) (err error) {
// em := NewManager("test")
// em.FireAsync("db.user.add", M{"id": 1001})
func (em *Manager) FireAsync(e Event) {
if em.ConsumerNum <= 0 {
em.ConsumerNum = defaultConsumerNum
}
if em.ChannelSize <= 0 {
em.ChannelSize = defaultChannelSize
}

// once make consumers
em.oc.Do(func() {
em.makeConsumers()
Expand All @@ -343,6 +336,13 @@ func (em *Manager) FireAsync(e Event) {

// async fire event by 'go' keywords
func (em *Manager) makeConsumers() {
if em.ConsumerNum <= 0 {
em.ConsumerNum = defaultConsumerNum
}
if em.ChannelSize <= 0 {
em.ChannelSize = defaultChannelSize
}

em.ch = make(chan Event, em.ChannelSize)

// make event consumers
Expand Down Expand Up @@ -450,7 +450,9 @@ func (em *Manager) AddEvent(e Event) {

// AddEventFc add a pre-defined event factory func to manager.
func (em *Manager) AddEventFc(name string, fc FactoryFunc) {
em.Lock()
em.eventFc[name] = fc
em.Unlock()
}

// GetEvent get a pre-defined event instance by name
Expand Down
2 changes: 1 addition & 1 deletion manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/gookit/event"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func TestManager_FireEvent(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion simpleevent/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

func TestEvent(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion std_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/gookit/event"
"github.com/stretchr/testify/assert"
"github.com/gookit/goutil/testutil/assert"
)

var emptyListener = func(e event.Event) error {
Expand Down

0 comments on commit b1597a2

Please sign in to comment.