From 04c7e8f687681174e3390c9de634626bfa1c4dd6 Mon Sep 17 00:00:00 2001 From: marusama Date: Wed, 13 Dec 2017 11:46:07 +0300 Subject: [PATCH] Fix go vet warnings in tests and up coverage to 100 --- README.md | 2 +- semaphore_test.go | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 19e6849..8584d5f 100644 --- a/README.md +++ b/README.md @@ -78,4 +78,4 @@ BenchmarkPivotalGolangSemaphore_Acquire_Release_under_limit-4 100 BenchmarkPivotalGolangSemaphore_Acquire_Release_over_limit-4 10000 128890 ns/op 12800 B/op 200 allocs/op ``` -You can rerun these benchmarks, just checkout `benchmarks` branch and do `go test -bench=. -benchmem ./bench/...` +You can rerun these benchmarks, just checkout `benchmarks` branch and run `go test -bench=. -benchmem ./bench/...` diff --git a/semaphore_test.go b/semaphore_test.go index e58cf40..db3cc18 100644 --- a/semaphore_test.go +++ b/semaphore_test.go @@ -132,12 +132,15 @@ func TestSemaphore_TryAcquire_contention(t *testing.T) { c := make(chan struct{}) wg := sync.WaitGroup{} - for i := 0; i < 100; i++ { + for i := 0; i < 10; i++ { wg.Add(1) go func() { <-c - if sem.TryAcquire(1) { - sem.Release(1) + for j := 0; j < 10000; j++ { + if sem.TryAcquire(1) { + runtime.Gosched() + sem.Release(1) + } } wg.Done() }() @@ -244,7 +247,8 @@ func TestSemaphore_Acquire_Release_under_limit(t *testing.T) { func TestSemaphore_Acquire_Release_under_limit_ctx_done(t *testing.T) { sem := New(10) - ctx, _ := context.WithTimeout(context.Background(), time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() c := make(chan struct{}) wg := sync.WaitGroup{} @@ -300,7 +304,8 @@ func TestSemaphore_Acquire_Release_over_limit(t *testing.T) { func TestSemaphore_Acquire_Release_over_limit_ctx_done(t *testing.T) { sem := New(1) - ctx, _ := context.WithTimeout(context.Background(), time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() c := make(chan struct{}) wg := sync.WaitGroup{} @@ -509,7 +514,8 @@ func TestSemaphore_Acquire_Release_SetLimit_under_limit(t *testing.T) { func TestSemaphore_Acquire_Release_SetLimit_under_limit_ctx_done(t *testing.T) { sem := New(100) - ctx, _ := context.WithTimeout(context.Background(), time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() c := make(chan struct{}) wg := sync.WaitGroup{} @@ -610,7 +616,8 @@ func TestSemaphore_Acquire_Release_SetLimit_over_limit(t *testing.T) { func TestSemaphore_Acquire_Release_SetLimit_over_limit_ctx_done(t *testing.T) { sem := New(1) - ctx, _ := context.WithTimeout(context.Background(), time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() c := make(chan struct{}) wg := sync.WaitGroup{} @@ -711,7 +718,8 @@ func TestSemaphore_Acquire_Release_SetLimit_random_limit(t *testing.T) { func TestSemaphore_Acquire_Release_SetLimit_random_limit_ctx_done(t *testing.T) { sem := New(1) - ctx, _ := context.WithTimeout(context.Background(), time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() c := make(chan struct{}) wg := sync.WaitGroup{}