github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/lang/channel/channel_example_test.go (about)

     1  // Copyright 2023 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package channel
    16  
    17  import (
    18  	"runtime"
    19  	"sync/atomic"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  type request struct {
    27  	Id      int
    28  	Latency time.Duration
    29  	Done    chan struct{}
    30  }
    31  
    32  type response struct {
    33  	Id int
    34  }
    35  
    36  var taskPool Channel
    37  
    38  func Service1(req *request) {
    39  	taskPool.Input(req)
    40  	return
    41  }
    42  
    43  func Service2(req *request) (*response, error) {
    44  	if req.Latency > 0 {
    45  		time.Sleep(req.Latency)
    46  	}
    47  	return &response{Id: req.Id}, nil
    48  }
    49  
    50  func TestNetworkIsolationOrDownstreamBlock(t *testing.T) {
    51  	taskPool = New(
    52  		WithNonBlock(),
    53  		WithTimeout(time.Millisecond*10),
    54  	)
    55  	defer taskPool.Close()
    56  	var responded int32
    57  	go func() {
    58  		// task worker
    59  		for task := range taskPool.Output() {
    60  			req := task.(*request)
    61  			done := make(chan struct{})
    62  			go func() {
    63  				_, _ = Service2(req)
    64  				close(done)
    65  			}()
    66  			select {
    67  			case <-time.After(time.Millisecond * 100):
    68  			case <-done:
    69  				atomic.AddInt32(&responded, 1)
    70  			}
    71  		}
    72  	}()
    73  
    74  	start := time.Now()
    75  	for i := 1; i <= 100; i++ {
    76  		req := &request{Id: i}
    77  		if i > 50 && i <= 60 { // suddenly have network issue for 10 requests
    78  			req.Latency = time.Hour
    79  		}
    80  		Service1(req)
    81  	}
    82  	cost := time.Now().Sub(start)
    83  	assert.True(t, cost < time.Millisecond*10) // Service1 should not block
    84  	time.Sleep(time.Millisecond * 1500)        // wait all tasks finished
    85  	assert.Equal(t, int32(50), responded)      // 50 success and 10 timeout and 40 discard
    86  }
    87  
    88  func TestCPUHeavy(t *testing.T) {
    89  	runtime.GOMAXPROCS(1)
    90  	var concurrency int32
    91  	taskPool = New(
    92  		WithNonBlock(),
    93  		WithThrottle(nil, func(c Channel) bool {
    94  			return atomic.LoadInt32(&concurrency) > 10
    95  		}),
    96  	)
    97  	defer taskPool.Close()
    98  	var responded int32
    99  	go func() {
   100  		// task worker
   101  		for task := range taskPool.Output() {
   102  			req := task.(*request)
   103  			t.Logf("NumGoroutine: %d", runtime.NumGoroutine())
   104  			go func() {
   105  				curConcurrency := atomic.AddInt32(&concurrency, 1)
   106  				defer atomic.AddInt32(&concurrency, -1)
   107  				if curConcurrency > 10 {
   108  					// concurrency too high, reuqest faild
   109  					return
   110  				}
   111  
   112  				atomic.AddInt32(&responded, 1)
   113  				if req.Id >= 11 && req.Id <= 20 {
   114  					start := time.Now()
   115  					for x := uint64(0); ; x++ {
   116  						if x%1000 == 0 {
   117  							if time.Now().Sub(start) >= 100*time.Millisecond {
   118  								return
   119  							}
   120  						}
   121  					}
   122  				}
   123  			}()
   124  		}
   125  	}()
   126  
   127  	start := time.Now()
   128  	for i := 1; i <= 100; i++ {
   129  		req := &request{Id: i}
   130  		Service1(req)
   131  	}
   132  	cost := time.Now().Sub(start)
   133  	assert.True(t, cost < time.Millisecond*10) // Service1 should not block
   134  	time.Sleep(time.Second * 2)                // wait all tasks finished
   135  	t.Logf("responded: %d", responded)         // most tasks success
   136  }