github.com/gogf/gf@v1.16.9/os/glog/glog_z_unit_concurrent_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package glog_test
     8  
     9  import (
    10  	"github.com/gogf/gf/os/gfile"
    11  	"github.com/gogf/gf/os/glog"
    12  	"github.com/gogf/gf/os/gtime"
    13  	"github.com/gogf/gf/test/gtest"
    14  	"github.com/gogf/gf/text/gstr"
    15  	"sync"
    16  	"testing"
    17  )
    18  
    19  func Test_Concurrent(t *testing.T) {
    20  	gtest.C(t, func(t *gtest.T) {
    21  		c := 1000
    22  		l := glog.New()
    23  		s := "@1234567890#"
    24  		f := "test.log"
    25  		p := gfile.TempDir(gtime.TimestampNanoStr())
    26  		t.Assert(l.SetPath(p), nil)
    27  		defer gfile.Remove(p)
    28  		wg := sync.WaitGroup{}
    29  		ch := make(chan struct{})
    30  		for i := 0; i < c; i++ {
    31  			wg.Add(1)
    32  			go func() {
    33  				defer wg.Done()
    34  				<-ch
    35  				l.File(f).Stdout(false).Print(s)
    36  			}()
    37  		}
    38  		close(ch)
    39  		wg.Wait()
    40  		content := gfile.GetContents(gfile.Join(p, f))
    41  		t.Assert(gstr.Count(content, s), c)
    42  	})
    43  }