github.com/wangyougui/gf/v2@v2.6.5/os/gfpool/gfpool_z_unit_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/wangyougui/gf.
     6  
     7  package gfpool_test
     8  
     9  import (
    10  	"context"
    11  	"os"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/wangyougui/gf/v2/os/gfile"
    16  	"github.com/wangyougui/gf/v2/os/gfpool"
    17  	"github.com/wangyougui/gf/v2/os/glog"
    18  	"github.com/wangyougui/gf/v2/os/gtime"
    19  	"github.com/wangyougui/gf/v2/test/gtest"
    20  	"github.com/wangyougui/gf/v2/text/gstr"
    21  )
    22  
    23  // TestOpen test open file cache
    24  func TestOpen(t *testing.T) {
    25  	testFile := start("TestOpen.txt")
    26  
    27  	gtest.C(t, func(t *gtest.T) {
    28  		f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
    29  		t.AssertEQ(err, nil)
    30  		f.Close()
    31  
    32  		f2, err1 := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
    33  		t.AssertEQ(err1, nil)
    34  		t.AssertEQ(f, f2)
    35  		f2.Close()
    36  	})
    37  
    38  	stop(testFile)
    39  }
    40  
    41  // TestOpenErr test open file error
    42  func TestOpenErr(t *testing.T) {
    43  	gtest.C(t, func(t *gtest.T) {
    44  		testErrFile := "errorPath"
    45  		_, err := gfpool.Open(testErrFile, os.O_RDWR, 0666)
    46  		t.AssertNE(err, nil)
    47  
    48  		// delete file error
    49  		testFile := start("TestOpenDeleteErr.txt")
    50  		pool := gfpool.New(testFile, os.O_RDWR, 0666)
    51  		_, err1 := pool.File()
    52  		t.AssertEQ(err1, nil)
    53  		stop(testFile)
    54  		_, err1 = pool.File()
    55  		t.AssertNE(err1, nil)
    56  
    57  		// append mode delete file error and create again
    58  		testFile = start("TestOpenCreateErr.txt")
    59  		pool = gfpool.New(testFile, os.O_CREATE, 0666)
    60  		_, err1 = pool.File()
    61  		t.AssertEQ(err1, nil)
    62  		stop(testFile)
    63  		_, err1 = pool.File()
    64  		t.AssertEQ(err1, nil)
    65  
    66  		// append mode delete file error
    67  		testFile = start("TestOpenAppendErr.txt")
    68  		pool = gfpool.New(testFile, os.O_APPEND, 0666)
    69  		_, err1 = pool.File()
    70  		t.AssertEQ(err1, nil)
    71  		stop(testFile)
    72  		_, err1 = pool.File()
    73  		t.AssertNE(err1, nil)
    74  
    75  		// trunc mode delete file error
    76  		testFile = start("TestOpenTruncErr.txt")
    77  		pool = gfpool.New(testFile, os.O_TRUNC, 0666)
    78  		_, err1 = pool.File()
    79  		t.AssertEQ(err1, nil)
    80  		stop(testFile)
    81  		_, err1 = pool.File()
    82  		t.AssertNE(err1, nil)
    83  	})
    84  }
    85  
    86  // TestOpenExpire test open file cache expire
    87  func TestOpenExpire(t *testing.T) {
    88  	testFile := start("TestOpenExpire.txt")
    89  
    90  	gtest.C(t, func(t *gtest.T) {
    91  		f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100*time.Millisecond)
    92  		t.AssertEQ(err, nil)
    93  		f.Close()
    94  
    95  		time.Sleep(150 * time.Millisecond)
    96  		f2, err1 := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100*time.Millisecond)
    97  		t.AssertEQ(err1, nil)
    98  		//t.AssertNE(f, f2)
    99  		f2.Close()
   100  	})
   101  
   102  	stop(testFile)
   103  }
   104  
   105  // TestNewPool test gfpool new function
   106  func TestNewPool(t *testing.T) {
   107  	testFile := start("TestNewPool.txt")
   108  
   109  	gtest.C(t, func(t *gtest.T) {
   110  		f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   111  		t.AssertEQ(err, nil)
   112  		f.Close()
   113  
   114  		pool := gfpool.New(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   115  		f2, err1 := pool.File()
   116  		// pool not equal
   117  		t.AssertEQ(err1, nil)
   118  		//t.AssertNE(f, f2)
   119  		f2.Close()
   120  
   121  		pool.Close()
   122  	})
   123  
   124  	stop(testFile)
   125  }
   126  
   127  // test before
   128  func start(name string) string {
   129  	testFile := os.TempDir() + string(os.PathSeparator) + name
   130  	if gfile.Exists(testFile) {
   131  		gfile.Remove(testFile)
   132  	}
   133  	content := "123"
   134  	gfile.PutContents(testFile, content)
   135  	return testFile
   136  }
   137  
   138  // test after
   139  func stop(testFile string) {
   140  	if gfile.Exists(testFile) {
   141  		err := gfile.Remove(testFile)
   142  		if err != nil {
   143  			glog.Error(context.TODO(), err)
   144  		}
   145  	}
   146  }
   147  
   148  func Test_ConcurrentOS(t *testing.T) {
   149  	gtest.C(t, func(t *gtest.T) {
   150  		path := gfile.Temp(gtime.TimestampNanoStr())
   151  		defer gfile.Remove(path)
   152  		f1, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   153  		t.AssertNil(err)
   154  		defer f1.Close()
   155  
   156  		f2, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   157  		t.AssertNil(err)
   158  		defer f2.Close()
   159  
   160  		for i := 0; i < 100; i++ {
   161  			_, err = f1.Write([]byte("@1234567890#"))
   162  			t.AssertNil(err)
   163  		}
   164  		for i := 0; i < 100; i++ {
   165  			_, err = f2.Write([]byte("@1234567890#"))
   166  			t.AssertNil(err)
   167  		}
   168  
   169  		for i := 0; i < 1000; i++ {
   170  			_, err = f1.Write([]byte("@1234567890#"))
   171  			t.AssertNil(err)
   172  		}
   173  		for i := 0; i < 1000; i++ {
   174  			_, err = f2.Write([]byte("@1234567890#"))
   175  			t.AssertNil(err)
   176  		}
   177  		t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2200)
   178  	})
   179  
   180  	gtest.C(t, func(t *gtest.T) {
   181  		path := gfile.Temp(gtime.TimestampNanoStr())
   182  		defer gfile.Remove(path)
   183  		f1, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   184  		t.AssertNil(err)
   185  		defer f1.Close()
   186  
   187  		f2, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   188  		t.AssertNil(err)
   189  		defer f2.Close()
   190  
   191  		for i := 0; i < 1000; i++ {
   192  			_, err = f1.Write([]byte("@1234567890#"))
   193  			t.AssertNil(err)
   194  		}
   195  		for i := 0; i < 1000; i++ {
   196  			_, err = f2.Write([]byte("@1234567890#"))
   197  			t.AssertNil(err)
   198  		}
   199  		t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
   200  	})
   201  	gtest.C(t, func(t *gtest.T) {
   202  		path := gfile.Temp(gtime.TimestampNanoStr())
   203  		defer gfile.Remove(path)
   204  		f1, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   205  		t.AssertNil(err)
   206  		defer f1.Close()
   207  
   208  		f2, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   209  		t.AssertNil(err)
   210  		defer f2.Close()
   211  
   212  		s1 := ""
   213  		for i := 0; i < 1000; i++ {
   214  			s1 += "@1234567890#"
   215  		}
   216  		_, err = f2.Write([]byte(s1))
   217  		t.AssertNil(err)
   218  
   219  		s2 := ""
   220  		for i := 0; i < 1000; i++ {
   221  			s2 += "@1234567890#"
   222  		}
   223  		_, err = f2.Write([]byte(s2))
   224  		t.AssertNil(err)
   225  
   226  		t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
   227  	})
   228  	// DATA RACE
   229  	// gtest.C(t, func(t *gtest.T) {
   230  	//	path := gfile.Temp(gtime.TimestampNanoStr())
   231  	//	defer gfile.Remove(path)
   232  	//	f1, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   233  	//	t.AssertNil(err)
   234  	//	defer f1.Close()
   235  	//
   236  	//	f2, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   237  	//	t.AssertNil(err)
   238  	//	defer f2.Close()
   239  	//
   240  	//	wg := sync.WaitGroup{}
   241  	//	ch := make(chan struct{})
   242  	//	for i := 0; i < 1000; i++ {
   243  	//		wg.Add(1)
   244  	//		go func() {
   245  	//			defer wg.Done()
   246  	//			<-ch
   247  	//			_, err = f1.Write([]byte("@1234567890#"))
   248  	//			t.AssertNil(err)
   249  	//		}()
   250  	//	}
   251  	//	for i := 0; i < 1000; i++ {
   252  	//		wg.Add(1)
   253  	//		go func() {
   254  	//			defer wg.Done()
   255  	//			<-ch
   256  	//			_, err = f2.Write([]byte("@1234567890#"))
   257  	//			t.AssertNil(err)
   258  	//		}()
   259  	//	}
   260  	//	close(ch)
   261  	//	wg.Wait()
   262  	//	t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
   263  	// })
   264  }
   265  
   266  func Test_ConcurrentGFPool(t *testing.T) {
   267  	gtest.C(t, func(t *gtest.T) {
   268  		path := gfile.Temp(gtime.TimestampNanoStr())
   269  		defer gfile.Remove(path)
   270  		f1, err := gfpool.Open(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   271  		t.AssertNil(err)
   272  		defer f1.Close()
   273  
   274  		f2, err := gfpool.Open(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   275  		t.AssertNil(err)
   276  		defer f2.Close()
   277  
   278  		for i := 0; i < 1000; i++ {
   279  			_, err = f1.Write([]byte("@1234567890#"))
   280  			t.AssertNil(err)
   281  		}
   282  		for i := 0; i < 1000; i++ {
   283  			_, err = f2.Write([]byte("@1234567890#"))
   284  			t.AssertNil(err)
   285  		}
   286  		t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
   287  	})
   288  	// DATA RACE
   289  	// gtest.C(t, func(t *gtest.T) {
   290  	//	path := gfile.Temp(gtime.TimestampNanoStr())
   291  	//	defer gfile.Remove(path)
   292  	//	f1, err := gfpool.Open(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   293  	//	t.AssertNil(err)
   294  	//	defer f1.Close()
   295  	//
   296  	//	f2, err := gfpool.Open(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   297  	//	t.AssertNil(err)
   298  	//	defer f2.Close()
   299  	//
   300  	//	wg := sync.WaitGroup{}
   301  	//	ch := make(chan struct{})
   302  	//	for i := 0; i < 1000; i++ {
   303  	//		wg.Add(1)
   304  	//		go func() {
   305  	//			defer wg.Done()
   306  	//			<-ch
   307  	//			_, err = f1.Write([]byte("@1234567890#"))
   308  	//			t.AssertNil(err)
   309  	//		}()
   310  	//	}
   311  	//	for i := 0; i < 1000; i++ {
   312  	//		wg.Add(1)
   313  	//		go func() {
   314  	//			defer wg.Done()
   315  	//			<-ch
   316  	//			_, err = f2.Write([]byte("@1234567890#"))
   317  	//			t.AssertNil(err)
   318  	//		}()
   319  	//	}
   320  	//	close(ch)
   321  	//	wg.Wait()
   322  	//	t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
   323  	// })
   324  }