File: deferblock_test.go

package info (click to toggle)
golang-github-gopherjs-gopherjs 0.0~git20170927.0.4152256-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,468 kB
  • sloc: cpp: 91; makefile: 7
file content (42 lines) | stat: -rw-r--r-- 662 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package tests

import (
	"testing"
	"time"
)

func inner(ch chan struct{}, b bool) ([]byte, error) {
	// ensure gopherjs thinks that this inner function can block
	if b {
		<-ch
	}
	return []byte{}, nil
}

// this function's call to inner never blocks, but the deferred
// statement does.
func outer(ch chan struct{}, b bool) ([]byte, error) {
	defer func() {
		<-ch
	}()

	return inner(ch, b)
}

func TestBlockingInDefer(t *testing.T) {
	defer func() {
		if x := recover(); x != nil {
			t.Errorf("run time panic: %v", x)
		}
	}()

	ch := make(chan struct{})
	b := false

	go func() {
		time.Sleep(5 * time.Millisecond)
		ch <- struct{}{}
	}()

	outer(ch, b)
}