github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/bfs/blocks_file_test.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package bfs_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/utils/bfs"
     7  	"github.com/iwind/TeaGo/assert"
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  func TestBlocksFile_CanClose(t *testing.T) {
    13  	var a = assert.NewAssertion(t)
    14  
    15  	bFile, openErr := bfs.OpenBlocksFile("testdata/test.b", bfs.DefaultBlockFileOptions)
    16  	if openErr != nil {
    17  		if os.IsNotExist(openErr) {
    18  			return
    19  		}
    20  		t.Fatal(openErr)
    21  	}
    22  
    23  	reader, err := bFile.OpenFileReader(bfs.Hash("123456"), false)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	a.IsTrue(!bFile.CanClose())
    29  
    30  	err = reader.Close()
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	// duplicated close
    36  	err = reader.Close()
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	a.IsTrue(bFile.CanClose())
    42  }
    43  
    44  func TestBlocksFile_OpenFileWriter_SameHash(t *testing.T) {
    45  	bFile, openErr := bfs.OpenBlocksFile("testdata/test.b", bfs.DefaultBlockFileOptions)
    46  	if openErr != nil {
    47  		if os.IsNotExist(openErr) {
    48  			return
    49  		}
    50  		t.Fatal(openErr)
    51  	}
    52  
    53  	{
    54  		writer, err := bFile.OpenFileWriter(bfs.Hash("123456"), -1, false)
    55  		if err != nil {
    56  			t.Fatal(err)
    57  		}
    58  		_ = writer.Close()
    59  	}
    60  
    61  	{
    62  		writer, err := bFile.OpenFileWriter(bfs.Hash("123456"), -1, false)
    63  		if err != nil {
    64  			t.Fatal(err)
    65  		}
    66  		_ = writer.Close()
    67  	}
    68  }
    69  
    70  func TestBlocksFile_RemoveAll(t *testing.T) {
    71  	bFile, err := bfs.OpenBlocksFile("testdata/test.b", bfs.DefaultBlockFileOptions)
    72  	if err != nil {
    73  		if os.IsNotExist(err) {
    74  			return
    75  		}
    76  		t.Fatal(err)
    77  	}
    78  	defer func() {
    79  		_ = bFile.Close()
    80  	}()
    81  
    82  	err = bFile.RemoveAll()
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  }