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