Created
July 28, 2021 09:47
-
-
Save ken39arg/22ec93eb3b25f38a949c607b02e81b4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"testing" | |
"golang.org/x/sync/errgroup" | |
) | |
type action func() error | |
type hasAction struct { | |
actions []action | |
} | |
func (h *hasAction) AddAction(act action) { | |
h.actions = append(h.actions, act) | |
} | |
func TestSlice(t *testing.T) { | |
if err := syncTest(t); err != nil { | |
t.Error(err) | |
} | |
for i := 0; i < 1000; i++ { | |
if err := asyncTest(t); err != nil { | |
t.Error(err) | |
} | |
} | |
} | |
func syncTest(t *testing.T) error { | |
h := &hasAction{} | |
for i := 0; i < 10; i++ { | |
n := i | |
h.AddAction(func() error { | |
log.Print(n) | |
return nil | |
}) | |
} | |
return nil | |
} | |
func asyncTest(t *testing.T) error { | |
var eg errgroup.Group | |
h := &hasAction{} | |
for i := 0; i < 10; i++ { | |
n := i | |
eg.Go(func() error { | |
h.AddAction(func() error { | |
log.Print(n) | |
return nil | |
}) | |
return nil | |
}) | |
} | |
return eg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
何回か回すと SEGVする