github.com/gogf/gf@v1.16.9/os/gfpool/gfpool_z_unit_test.go (about)

     1  package gfpool_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/gogf/gf/os/gfile"
     9  	"github.com/gogf/gf/os/gfpool"
    10  	"github.com/gogf/gf/os/glog"
    11  	"github.com/gogf/gf/test/gtest"
    12  )
    13  
    14  // TestOpen test open file cache
    15  func TestOpen(t *testing.T) {
    16  	testFile := start("TestOpen.txt")
    17  
    18  	gtest.C(t, func(t *gtest.T) {
    19  		f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
    20  		t.AssertEQ(err, nil)
    21  		f.Close()
    22  
    23  		f2, err1 := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
    24  		t.AssertEQ(err1, nil)
    25  		t.AssertEQ(f, f2)
    26  		f2.Close()
    27  	})
    28  
    29  	stop(testFile)
    30  }
    31  
    32  // TestOpenErr test open file error
    33  func TestOpenErr(t *testing.T) {
    34  	gtest.C(t, func(t *gtest.T) {
    35  		testErrFile := "errorPath"
    36  		_, err := gfpool.Open(testErrFile, os.O_RDWR, 0666)
    37  		t.AssertNE(err, nil)
    38  
    39  		// delete file error
    40  		testFile := start("TestOpenDeleteErr.txt")
    41  		pool := gfpool.New(testFile, os.O_RDWR, 0666)
    42  		_, err1 := pool.File()
    43  		t.AssertEQ(err1, nil)
    44  		stop(testFile)
    45  		_, err1 = pool.File()
    46  		t.AssertNE(err1, nil)
    47  
    48  		// append mode delete file error and create again
    49  		testFile = start("TestOpenCreateErr.txt")
    50  		pool = gfpool.New(testFile, os.O_CREATE, 0666)
    51  		_, err1 = pool.File()
    52  		t.AssertEQ(err1, nil)
    53  		stop(testFile)
    54  		_, err1 = pool.File()
    55  		t.AssertEQ(err1, nil)
    56  
    57  		// append mode delete file error
    58  		testFile = start("TestOpenAppendErr.txt")
    59  		pool = gfpool.New(testFile, os.O_APPEND, 0666)
    60  		_, err1 = pool.File()
    61  		t.AssertEQ(err1, nil)
    62  		stop(testFile)
    63  		_, err1 = pool.File()
    64  		t.AssertNE(err1, nil)
    65  
    66  		// trunc mode delete file error
    67  		testFile = start("TestOpenTruncErr.txt")
    68  		pool = gfpool.New(testFile, os.O_TRUNC, 0666)
    69  		_, err1 = pool.File()
    70  		t.AssertEQ(err1, nil)
    71  		stop(testFile)
    72  		_, err1 = pool.File()
    73  		t.AssertNE(err1, nil)
    74  	})
    75  }
    76  
    77  // TestOpenExpire test open file cache expire
    78  func TestOpenExpire(t *testing.T) {
    79  	testFile := start("TestOpenExpire.txt")
    80  
    81  	gtest.C(t, func(t *gtest.T) {
    82  		f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100*time.Millisecond)
    83  		t.AssertEQ(err, nil)
    84  		f.Close()
    85  
    86  		time.Sleep(150 * time.Millisecond)
    87  		f2, err1 := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666, 100*time.Millisecond)
    88  		t.AssertEQ(err1, nil)
    89  		//t.AssertNE(f, f2)
    90  		f2.Close()
    91  	})
    92  
    93  	stop(testFile)
    94  }
    95  
    96  // TestNewPool test gfpool new function
    97  func TestNewPool(t *testing.T) {
    98  	testFile := start("TestNewPool.txt")
    99  
   100  	gtest.C(t, func(t *gtest.T) {
   101  		f, err := gfpool.Open(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   102  		t.AssertEQ(err, nil)
   103  		f.Close()
   104  
   105  		pool := gfpool.New(testFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
   106  		f2, err1 := pool.File()
   107  		// pool not equal
   108  		t.AssertEQ(err1, nil)
   109  		//t.AssertNE(f, f2)
   110  		f2.Close()
   111  
   112  		pool.Close()
   113  	})
   114  
   115  	stop(testFile)
   116  }
   117  
   118  // test before
   119  func start(name string) string {
   120  	testFile := os.TempDir() + string(os.PathSeparator) + name
   121  	if gfile.Exists(testFile) {
   122  		gfile.Remove(testFile)
   123  	}
   124  	content := "123"
   125  	gfile.PutContents(testFile, content)
   126  	return testFile
   127  }
   128  
   129  // test after
   130  func stop(testFile string) {
   131  	if gfile.Exists(testFile) {
   132  		err := gfile.Remove(testFile)
   133  		if err != nil {
   134  			glog.Error(err)
   135  		}
   136  	}
   137  }