Brad Fitzpatrick | 6fe7631 | 2014-11-11 09:26:28 -0600 | [diff] [blame] | 1 | // Copyright 2014 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
Brad Fitzpatrick | 6fe7631 | 2014-11-11 09:26:28 -0600 | [diff] [blame] | 4 | |
| 5 | package http2 |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestGoroutineLock(t *testing.T) { |
Jeff R. Allen | 6c89489 | 2015-12-19 16:51:57 +0600 | [diff] [blame] | 14 | oldDebug := DebugGoroutines |
Brad Fitzpatrick | 6fe7631 | 2014-11-11 09:26:28 -0600 | [diff] [blame] | 15 | DebugGoroutines = true |
Jeff R. Allen | 6c89489 | 2015-12-19 16:51:57 +0600 | [diff] [blame] | 16 | defer func() { DebugGoroutines = oldDebug }() |
| 17 | |
Brad Fitzpatrick | 6fe7631 | 2014-11-11 09:26:28 -0600 | [diff] [blame] | 18 | g := newGoroutineLock() |
| 19 | g.check() |
| 20 | |
| 21 | sawPanic := make(chan interface{}) |
| 22 | go func() { |
| 23 | defer func() { sawPanic <- recover() }() |
| 24 | g.check() // should panic |
| 25 | }() |
| 26 | e := <-sawPanic |
| 27 | if e == nil { |
| 28 | t.Fatal("did not see panic from check in other goroutine") |
| 29 | } |
| 30 | if !strings.Contains(fmt.Sprint(e), "wrong goroutine") { |
| 31 | t.Errorf("expected on see panic about running on the wrong goroutine; got %v", e) |
| 32 | } |
| 33 | } |