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

     1  package tests
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func inner(ch chan struct{}, b bool) ([]byte, error) {
     9  	// ensure gopherjs thinks that this inner function can block
    10  	if b {
    11  		<-ch
    12  	}
    13  	return []byte{}, nil
    14  }
    15  
    16  // this function's call to inner never blocks, but the deferred
    17  // statement does.
    18  func outer(ch chan struct{}, b bool) ([]byte, error) {
    19  	defer func() {
    20  		<-ch
    21  	}()
    22  
    23  	return inner(ch, b)
    24  }
    25  
    26  func TestBlockingInDefer(t *testing.T) {
    27  	defer func() {
    28  		if x := recover(); x != nil {
    29  			t.Errorf("run time panic: %v", x)
    30  		}
    31  	}()
    32  
    33  	ch := make(chan struct{})
    34  	b := false
    35  
    36  	go func() {
    37  		time.Sleep(5 * time.Millisecond)
    38  		ch <- struct{}{}
    39  	}()
    40  
    41  	outer(ch, b)
    42  }