github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/tests/goroutine_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  var expectedI int
    10  
    11  func checkI(t *testing.T, i int) {
    12  	if i != expectedI {
    13  		t.Errorf("expected %d, got %d", expectedI, i)
    14  	}
    15  	expectedI++
    16  }
    17  
    18  func TestDefer(t *testing.T) {
    19  	expectedI = 1
    20  	defer func() {
    21  		checkI(t, 2)
    22  		testDefer1(t)
    23  		checkI(t, 6)
    24  	}()
    25  	checkI(t, 1)
    26  }
    27  
    28  func testDefer1(t *testing.T) {
    29  	defer func() {
    30  		checkI(t, 4)
    31  		time.Sleep(0)
    32  		checkI(t, 5)
    33  	}()
    34  	checkI(t, 3)
    35  }
    36  
    37  func TestPanic(t *testing.T) {
    38  	expectedI = 1
    39  	defer func() {
    40  		checkI(t, 8)
    41  		err := recover()
    42  		time.Sleep(0)
    43  		checkI(t, err.(int))
    44  	}()
    45  	checkI(t, 1)
    46  	testPanic1(t)
    47  	checkI(t, -1)
    48  }
    49  
    50  func testPanic1(t *testing.T) {
    51  	defer func() {
    52  		checkI(t, 6)
    53  		time.Sleep(0)
    54  		err := recover()
    55  		checkI(t, err.(int))
    56  		panic(9)
    57  	}()
    58  	checkI(t, 2)
    59  	testPanic2(t)
    60  	checkI(t, -2)
    61  }
    62  
    63  func testPanic2(t *testing.T) {
    64  	defer func() {
    65  		checkI(t, 5)
    66  	}()
    67  	checkI(t, 3)
    68  	time.Sleep(0)
    69  	checkI(t, 4)
    70  	panic(7)
    71  	checkI(t, -3)
    72  }
    73  
    74  func TestPanicAdvanced(t *testing.T) {
    75  	expectedI = 1
    76  	defer func() {
    77  		recover()
    78  		checkI(t, 3)
    79  		testPanicAdvanced2(t)
    80  		checkI(t, 6)
    81  	}()
    82  	testPanicAdvanced1(t)
    83  	checkI(t, -1)
    84  }
    85  
    86  func testPanicAdvanced1(t *testing.T) {
    87  	defer func() {
    88  		checkI(t, 2)
    89  	}()
    90  	checkI(t, 1)
    91  	panic("")
    92  }
    93  
    94  func testPanicAdvanced2(t *testing.T) {
    95  	defer func() {
    96  		checkI(t, 5)
    97  	}()
    98  	checkI(t, 4)
    99  }
   100  
   101  func TestSelect(t *testing.T) {
   102  	expectedI = 1
   103  	a := make(chan int)
   104  	b := make(chan int)
   105  	c := make(chan int)
   106  	go func() {
   107  		select {
   108  		case <-a:
   109  		case <-b:
   110  		}
   111  	}()
   112  	go func() {
   113  		checkI(t, 1)
   114  		a <- 1
   115  		select {
   116  		case b <- 1:
   117  			checkI(t, -1)
   118  		default:
   119  			checkI(t, 2)
   120  		}
   121  		c <- 1
   122  	}()
   123  	<-c
   124  	checkI(t, 3)
   125  }
   126  
   127  func TestCloseAfterReceiving(t *testing.T) {
   128  	ch := make(chan struct{})
   129  	go func() {
   130  		<-ch
   131  		close(ch)
   132  	}()
   133  	ch <- struct{}{}
   134  }
   135  
   136  func TestDeferWithBlocking(t *testing.T) {
   137  	ch := make(chan struct{})
   138  	go func() { ch <- struct{}{} }()
   139  	defer func() { <-ch }()
   140  	fmt.Print("")
   141  	return
   142  }