Пример #1
0
func TestNoRaceProducerConsumerUnbuffered(t *testing.T) {
	type Task struct {
		f    func()
		done chan bool
	}

	queue := make(chan Task)

	go func() {
		t := <-queue
		t.f()
		t.done <- true
	}()

	doit := func(f func()) {
		done := make(chan bool, 1)
		queue <- Task{f, done}
		<-done
	}

	x := 0
	doit(func() {
		x = 1
	})
	_ = x
}
Пример #2
0
func TestNoRaceSelect4(t *testing.T) {
	type Task struct {
		f    func()
		done chan bool
	}

	queue := make(chan Task)
	dummy := make(chan bool)

	go func() {
		for {
			select {
			case t := <-queue:
				t.f()
				t.done <- true
			}
		}
	}()

	doit := func(f func()) {
		done := make(chan bool, 1)
		select {
		case queue <- Task{f, done}:
		case <-dummy:
		}
		select {
		case <-done:
		case <-dummy:
		}
	}

	var x int
	doit(func() {
		x = 1
	})
	_ = x
}