github.com/kaydxh/golang@v0.0.131/go/sync/cond_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package sync_test
    23  
    24  import (
    25  	"fmt"
    26  	"sync"
    27  	"testing"
    28  	"time"
    29  
    30  	sync_ "github.com/kaydxh/golang/go/sync"
    31  	"github.com/stretchr/testify/assert"
    32  )
    33  
    34  func TestWaitForDo(t *testing.T) {
    35  	assert := assert.New(t)
    36  
    37  	l := new(sync.Mutex)
    38  	cond := sync_.NewCond(l)
    39  	a := 5
    40  	timout := 4 * time.Second
    41  
    42  	var wg sync.WaitGroup
    43  	wg.Add(1)
    44  	go func() {
    45  		defer wg.Done()
    46  		err := cond.WaitForDo(timout, func() bool {
    47  			fmt.Printf("==>a = %v\n", a)
    48  			return a == 3
    49  		}, func() error {
    50  			a += 100
    51  			fmt.Printf("a: %v\n", a)
    52  			assert.Equal(103, a)
    53  			return nil
    54  		})
    55  		assert.Equal(nil, err)
    56  	}()
    57  
    58  	go func() {
    59  		for {
    60  			cond.SignalDo(func() error {
    61  				a--
    62  				//this sleep can increase the probability to waitforDo
    63  				//miss condition = > a==3
    64  				time.Sleep(1 * time.Second)
    65  				fmt.Printf("a: %v\n", a)
    66  				return nil
    67  			})
    68  			//time.Sleep(1 * time.Second)
    69  		}
    70  	}()
    71  
    72  	wg.Wait()
    73  }
    74  
    75  // condion only be fit for range condition
    76  func TestWaitUntilDo(t *testing.T) {
    77  	assert := assert.New(t)
    78  
    79  	l := new(sync.Mutex)
    80  	cond := sync_.NewCond(l)
    81  	a := 5
    82  
    83  	var wg sync.WaitGroup
    84  	wg.Add(1)
    85  	go func() {
    86  		defer wg.Done()
    87  		cond.WaitUntilDo(func() bool {
    88  			fmt.Printf("==>a = %v\n", a)
    89  			return a < 0
    90  		}, func() error {
    91  			fmt.Printf("a: %v\n", a)
    92  			assert.LessOrEqual(a, 0)
    93  			return nil
    94  		})
    95  	}()
    96  
    97  	go func() {
    98  		for {
    99  			cond.SignalDo(func() error {
   100  				a--
   101  				//this sleep can increase the probability to waitforDo
   102  				//miss condition = > a==3
   103  				time.Sleep(1 * time.Second)
   104  				fmt.Printf("a: %v\n", a)
   105  				return nil
   106  			})
   107  			//time.Sleep(1 * time.Second)
   108  		}
   109  	}()
   110  
   111  	wg.Wait()
   112  }
   113  
   114  func TestBroadCast(t *testing.T) {
   115  	assert := assert.New(t)
   116  
   117  	l := new(sync.Mutex)
   118  	cond := sync_.NewCond(l)
   119  
   120  	initValue := 10
   121  	expected := 3
   122  	threads := 5
   123  
   124  	var wg sync.WaitGroup
   125  
   126  	for i := 0; i < threads; i++ {
   127  		wg.Add(1)
   128  		go func() {
   129  			defer wg.Done()
   130  			cond.WaitForDo(10*time.Second, func() bool {
   131  				return expected == initValue
   132  			}, func() error {
   133  				assert.Equal(initValue, expected)
   134  				t.Logf("wait done")
   135  				return nil
   136  			})
   137  		}()
   138  	}
   139  
   140  	go func() {
   141  		for {
   142  			time.Sleep(time.Second)
   143  			cond.BroadcastDo(func() error {
   144  				initValue--
   145  				t.Logf("init: %v\n", initValue)
   146  				return nil
   147  			})
   148  		}
   149  	}()
   150  
   151  	wg.Wait()
   152  }