Skip to content

Instantly share code, notes, and snippets.

@ken39arg
Created July 28, 2021 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ken39arg/22ec93eb3b25f38a949c607b02e81b4a to your computer and use it in GitHub Desktop.
Save ken39arg/22ec93eb3b25f38a949c607b02e81b4a to your computer and use it in GitHub Desktop.
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()
}
@ken39arg
Copy link
Author

何回か回すと SEGVする

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x110da0b]

goroutine 137137 [running]:
command-line-arguments.(*hasAction).AddAction(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment