Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .github/workflows/codetests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
os: [macos, windows, ubuntu]
runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: 1.19
go-version: stable
- name: go-test
run: go test -race -covermode=atomic '-test.v' ./...
# Runs golangci-lint on macos against freebsd and macos.
Expand All @@ -29,14 +29,14 @@ jobs:
env:
GOOS: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v4
- uses: actions/setup-go@v6
with:
go-version: 1.19
- uses: actions/checkout@v4
go-version: stable
- uses: actions/checkout@v6
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v9
with:
version: v1.50
version: v2.9
# Runs golangci-lint on linux against linux and windows.
golangci-linux:
strategy:
Expand All @@ -47,11 +47,11 @@ jobs:
env:
GOOS: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v4
- uses: actions/setup-go@v6
with:
go-version: 1.19
- uses: actions/checkout@v4
go-version: stable
- uses: actions/checkout@v6
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v9
with:
version: v1.50
version: v2.9
50 changes: 28 additions & 22 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
issues:
exclude-rules:
- linters:
- testpackage
- gochecknoglobals
path: '(.+)_test\.go'
version: '2'
linters:
enable-all: true
default: all
disable:
# deprecated
- maligned
- scopelint
- interfacer
- golint
- exhaustivestruct
- nosnakecase
- structcheck
- deadcode
- varcheck
- ifshort
# unused
- exhaustruct
- nlreturn
run:
timeout: 3m
- depguard
- wsl
- testpackage
settings:
gocritic:
enable-all: true
settings:
unnamedResult:
checkExported: true
errcheck:
check-type-assertions: true
check-blank: false
disable-default-exclusions: false
exclude-functions:
- (*os.File).Close
- (io.Closer).Close

issues:
max-issues-per-linter: 0
max-same-issues: 0
formatters:
enable:
- gci
- gofmt
- gofumpt
- goimports
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 David Newhall II
Copyright (c) 2019-2026 David Newhall II

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
128 changes: 128 additions & 0 deletions concurrency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package subscribe

import (
"errors"
"path/filepath"
"strconv"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestEventsConcurrentAccess(t *testing.T) {
t.Parallel()

events := &Events{Map: make(map[string]*Rules)}
require.NoError(t, events.New("event", nil))

var waitGroup sync.WaitGroup

for i := range 32 {
waitGroup.Go(func() {
rule := "rule_" + strconv.Itoa(i)
for j := range 200 {
events.RuleSetI("event", rule, j)
events.RuleSetS("event", rule, rule)
events.RuleSetD("event", rule, time.Duration(j)*time.Millisecond)
events.RuleSetT("event", rule, time.Now().Add(time.Duration(j)*time.Second))

err := events.Pause("event", time.Millisecond)
if err != nil {
t.Errorf("pause failed: %v", err)

return
}

err = events.UnPause("event")
if err != nil {
t.Errorf("unpause failed: %v", err)

return
}

events.IsPaused("event")
events.PauseTime("event")
events.RuleGetI("event", rule)
events.RuleGetS("event", rule)
events.RuleGetD("event", rule)
events.RuleGetT("event", rule)
events.RuleDelAll("event", rule)
}
})
}

waitGroup.Wait()
}

func TestSubscribeConcurrentAccess(t *testing.T) {
t.Parallel()

stateFile := filepath.Join(t.TempDir(), "state.json")
sub, err := GetDB(stateFile)
require.NoError(t, err)
require.NoError(t, sub.Events.New("evt", nil))

var waitGroup sync.WaitGroup

for i := range 20 {
waitGroup.Go(func() {
contact := "contact_" + strconv.Itoa(i%5)
runSubscribeOps(t, sub, contact)
})
}

waitGroup.Wait()
}

func runSubscribeOps(t *testing.T, sub *Subscribe, contact string) {
t.Helper()

for j := range 100 {
admin := j%2 == 0
ignored := j%3 == 0
subscriber := sub.CreateSub(contact, "api", admin, ignored)

if subscriber == nil {
t.Error("subscriber should not be nil")

return
}

err := subscriber.Subscribe("evt")

if err != nil && !errors.Is(err, ErrEventExists) {
t.Errorf("subscribe failed: %v", err)

return
}

err = subscriber.Events.Pause("evt", time.Millisecond)
if err != nil {
t.Errorf("pause failed: %v", err)

return
}

err = subscriber.Events.UnPause("evt")
if err != nil {
t.Errorf("unpause failed: %v", err)

return
}

_, _ = sub.GetSubscriber(contact, "api")
sub.GetAdmins()
sub.GetIgnored()
sub.GetSubscribers("evt")
_, _ = sub.StateGetJSON()

err = sub.StateFileSave()
if err != nil {
t.Errorf("state save failed: %v", err)

return
}
}
}
Loading
Loading