Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

potential race condition #2

Open
lestrrat opened this issue Sep 16, 2016 · 1 comment
Open

potential race condition #2

lestrrat opened this issue Sep 16, 2016 · 1 comment

Comments

@lestrrat
Copy link

以下のテストを追加してgo test -race すると色々でてきます。

そもそもこういう使い方はありえないのかもしれませんが、最近こういうのを自分のコードで見つけるのに凝ってるので…

diff --git a/shapeio_test.go b/shapeio_test.go
index bd9b545..32d4e09 100644
--- a/shapeio_test.go
+++ b/shapeio_test.go
@@ -2,10 +2,12 @@ package shapeio_test

 import (
        "bytes"
+       "context"
        "io"
        "io/ioutil"
        "net/http"
        "os"
+       "sync"
        "testing"
        "time"

@@ -101,3 +103,50 @@ func TestWrite(t *testing.T) {
                }
        }
 }
+
+func TestConcurrentSetRateLimit(t *testing.T) {
+       // run with go test -race
+       ctx, cancel := context.WithCancel(context.Background())
+       var wg sync.WaitGroup
+
+       sio := shapeio.NewWriter(ioutil.Discard)
+
+       for _, l := range rates {
+               limit := l
+               wg.Add(1)
+               go func() {
+                       defer wg.Done()
+                       t := time.NewTicker(50 * time.Millisecond)
+                       defer t.Stop()
+                       for {
+                               select {
+                               case <-ctx.Done():
+                                       return
+                               case <-t.C:
+                                       sio.SetRateLimit(limit)
+                               }
+                       }
+               }()
+       }
+
+       wg.Add(1)
+       go func() {
+               defer wg.Done()
+               t := time.NewTicker(50 * time.Millisecond)
+               defer t.Stop()
+               for {
+                       select {
+                       case <-ctx.Done():
+                               return
+                       case <-t.C:
+                               for _, src := range srcs {
+                                       io.Copy(sio, src)
+                               }
+                       }
+               }
+       }()
+
+       time.AfterFunc(time.Second, cancel)
+
+       wg.Wait()
+}
@porjo
Copy link

porjo commented Jan 28, 2019

I don't think this is an issue with shapeio. It is just a simple library - it is up to the caller to handle synchronisation issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants