...

Source file src/github.com/modern-go/concurrent/unbounded_executor_test.go

Documentation: github.com/modern-go/concurrent

     1  package concurrent_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  	"github.com/modern-go/concurrent"
     8  )
     9  
    10  func ExampleUnboundedExecutor_Go() {
    11  	executor := concurrent.NewUnboundedExecutor()
    12  	executor.Go(func(ctx context.Context) {
    13  		fmt.Println("abc")
    14  	})
    15  	time.Sleep(time.Second)
    16  	// output: abc
    17  }
    18  
    19  func ExampleUnboundedExecutor_StopAndWaitForever() {
    20  	executor := concurrent.NewUnboundedExecutor()
    21  	executor.Go(func(ctx context.Context) {
    22  		everyMillisecond := time.NewTicker(time.Millisecond)
    23  		for {
    24  			select {
    25  			case <-ctx.Done():
    26  				fmt.Println("goroutine exited")
    27  				return
    28  			case <-everyMillisecond.C:
    29  				// do something
    30  			}
    31  		}
    32  	})
    33  	time.Sleep(time.Second)
    34  	executor.StopAndWaitForever()
    35  	fmt.Println("executor stopped")
    36  	// output:
    37  	// goroutine exited
    38  	// executor stopped
    39  }
    40  
    41  func ExampleUnboundedExecutor_Go_panic() {
    42  	concurrent.HandlePanic = func(recovered interface{}, funcName string) {
    43  		fmt.Println(funcName)
    44  	}
    45  	executor := concurrent.NewUnboundedExecutor()
    46  	executor.Go(willPanic)
    47  	time.Sleep(time.Second)
    48  	// output:
    49  	// github.com/modern-go/concurrent_test.willPanic
    50  }
    51  
    52  func willPanic(ctx context.Context) {
    53  	panic("!!!")
    54  }
    55  

View as plain text