Skip to content

TestGetDieCh() strange behaviour seems cause by racing #88

@cs8425

Description

@cs8425

run test with: go test -coverprofile=coverage.txt -covermode=atomic -v -run=TestGetDieCh .

in session_test.go:

func TestGetDieCh(t *testing.T) {
	cs, ss, err := getSmuxStreamPair()
	if err != nil {
		t.Fatal(err)
	}
	defer ss.Close()
	dieCh := ss.GetDieCh()
	go func() {
		t.Log("start")
		select {
		case <-dieCh:
		case <-time.Tick(time.Second):
			t.Fatal("wait die chan timeout")
		}
		t.Log("end")
	}()
	cs.Close()
	t.Log("wait")
}

will got:

=== RUN   TestGetDieCh
    session_test.go:298: start
    session_test.go:307: wait
--- PASS: TestGetDieCh (0.00s)
PASS
coverage: 32.5% of statements
ok  	github.com/xtaci/smux	0.004s	coverage: 32.5% of statements

and

=== RUN   TestGetDieCh
    session_test.go:307: wait
--- PASS: TestGetDieCh (0.00s)
PASS
panic: Log in goroutine after TestGetDieCh has completed: end


goroutine 27 [running]:
testing.(*common).logDepth(0xc000121380, {0xc00021c030, 0x4}, 0x3)
	/usr/local/go/src/testing/testing.go:779 +0x4c9
testing.(*common).log(...)
	/usr/local/go/src/testing/testing.go:761
testing.(*common).Log(0xc000215fb0, {0xc000215fa0, 0x0, 0x0})
	/usr/local/go/src/testing/testing.go:800 +0x45
github.com/xtaci/smux.TestGetDieCh.func1()
	/path-to-code/smux/session_test.go:304 +0x11f
created by github.com/xtaci/smux.TestGetDieCh
	/path-to-code/smux/session_test.go:297 +0x112
FAIL	github.com/xtaci/smux	0.006s
FAIL

if we change to this, be sure that test exit after goroutine:

func TestGetDieCh(t *testing.T) {
	cs, ss, err := getSmuxStreamPair()
	if err != nil {
		t.Fatal(err)
	}
	defer ss.Close()
	dieCh := ss.GetDieCh()

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()

		t.Log("start")
		select {
		case <-dieCh:
			t.Log("end")
		case <-time.Tick(time.Second):
			t.Fatal("wait die chan timeout")
		}
	}()
	cs.Close()

	t.Log("wait...")
	wg.Wait()
}

the test will fail with:

=== RUN   TestGetDieCh
    session_test.go:313: wait...
    session_test.go:303: start
    session_test.go:308: wait die chan timeout
--- FAIL: TestGetDieCh (1.00s)
FAIL
coverage: 32.5% of statements
FAIL	github.com/xtaci/smux	1.005s
FAIL

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions