Skip to content

Commit

Permalink
Merge pull request #9 from rookiecj/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rookiecj authored Jan 7, 2024
2 parents a098fc2 + c5392bf commit 49ebcc1
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 17 deletions.
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ tidy: ## update deps
go mod tidy

build: ## build
go build ./store/...
go build $(shell go list ./... | grep -v /example)

lint:
go vet ./store/...
lint: ## lint
go vet ./...

clean: ## clean
-rm store.test
go clean -cache


test: clean ## test
go test -v -timeout=10s ./store/...
go test -v -timeout=10s $(shell go list ./... | grep -v /example)

bench: clean ## test bench
# -benchtime sets the minimum amount of time that the benchmark function will run
# -run=^# filter out all of the unit test functions.
go test -v -bench=. -benchtime=10s ./store/...
go test -v -bench=. -benchtime=10s $(shell go list ./... | grep -v /example)

bench-mem: clean ## test bench with memory usage
# -run=^# filter out all of the unit test functions.
go test -v -bench=. -benchtime=10s -benchmem ./store/...
go test -v -bench=. -benchtime=10s -benchmem $(shell go list ./... | grep -v /example)

coverage: ## test with coverage
#go test --converage ./store/...
go test -coverprofile=coverage.txt -covermode=atomic -v -count=1 -timeout=30s -parallel=4 -failfast ./store/...


go test -coverprofile=coverage.txt -covermode=atomic -v -count=1 -timeout=30s -parallel=4 -failfast $(shell go list ./... | grep -v /example)
4 changes: 4 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func SetLogEnable(enable bool) {
logEnabled = enable
}

func LogForcedf(fmt string, args ...interface{}) {
log.Printf(fmt, args...)
}

func Logf(level string, fmt string, args ...interface{}) {
if !logEnabled {
return
Expand Down
8 changes: 6 additions & 2 deletions sched/sched_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (c *mainScheduler) Start() {
wg := sync.WaitGroup{}

wg.Add(1)
// WaitForScheduler to wait
c.doneWG.Add(1)
go func() {
wg.Done()
logger.Infof("mainScheduler:")
Expand All @@ -40,7 +42,10 @@ func (c *mainScheduler) Start() {
task()
}
}
logger.Infof("mainScheduler: exit remains %d", c.taskQ.Len())
if remains := c.taskQ.Len(); remains != 0 {
logger.LogForcedf("mainScheduler: exit remains %d", remains)
}
logger.Infof("mainScheduler: exit")
c.doneWG.Done()
}()
wg.Wait()
Expand All @@ -50,7 +55,6 @@ func (c *mainScheduler) Stop() {
if c == nil {
return
}
c.doneWG.Add(1)
go func() {
c.taskQ.Push(func() {
c.taskCount--
Expand Down
79 changes: 79 additions & 0 deletions sched/sched_main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package sched

import (
"testing"
)

func Test_mainScheduler_Schedule(t *testing.T) {
type args struct {
concurrent int
task TaskFunc
}

limit := 1_000_000

sharedVariableWithNoLock := 0

tests := []struct {
name string
s Scheduler
args args
want int
}{
{
name: "no tasks",
s: NewMainScheduler(),
args: args{
concurrent: 0,
task: func() {
sharedVariableWithNoLock++
},
},
want: 0,
},

{
name: "1 task",
s: NewMainScheduler(),
args: args{
concurrent: 1,
task: func() {
sharedVariableWithNoLock++
},
},
want: 1,
},

{
name: "concurrent tasks - schedule them on same context",
s: NewMainScheduler(),
args: args{
concurrent: limit,
task: func() {
sharedVariableWithNoLock++
},
},
want: limit,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := tt.s

sharedVariableWithNoLock = 0
c.Start()

for idx := 0; idx < tt.args.concurrent; idx++ {
c.Schedule(tt.args.task)
}

c.Stop()
c.WaitForScheduler()

got := sharedVariableWithNoLock
if tt.want != got {
t.Errorf("Schedule want %v got %v", tt.want, got)
}
})
}
}
11 changes: 6 additions & 5 deletions store/asyncaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/rookiecj/go-store/logger"
"math/rand"
"sync/atomic"
"testing"
"time"
)
Expand Down Expand Up @@ -108,11 +109,11 @@ func Test_AsyncAction_Run(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
setActionCalled := 0
var setActionCalled int64
tt.b.Subscribe(func(newState myState, oldState myState, action Action) {
switch action.(type) {
case *setAction:
setActionCalled++
atomic.AddInt64(&setActionCalled, 1)
}
})

Expand All @@ -125,8 +126,8 @@ func Test_AsyncAction_Run(t *testing.T) {
tt.b.Dispatch(action)
}

// give time to async action
time.Sleep(time.Duration(delay) * time.Millisecond)
// give enough(*2) time to async action
time.Sleep(time.Duration(delay) * 2 * time.Millisecond)

tt.b.waitForDispatch()

Expand All @@ -135,7 +136,7 @@ func Test_AsyncAction_Run(t *testing.T) {
//t.Errorf("AsyncAction_Run: want %d, got %d", tt.want, diff)
//}

if setActionCalled != tt.want {
if setActionCalled != int64(tt.want) {
t.Errorf("AsyncAction_Run: done action call want %d times but %d", tt.want, setActionCalled)
}

Expand Down
2 changes: 1 addition & 1 deletion store/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type setAction struct {
}

var (
testScheduler = sched.NewMainScheduler()
testScheduler = sched.NewMainScheduler() // sched.Main
myInitialState = myState{
id: 0,
value: "",
Expand Down

0 comments on commit 49ebcc1

Please sign in to comment.