github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/bfs/fs_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/TeaOSLab/EdgeNode/internal/utils/fasttime"
     8  	"github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist"
     9  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
    10  	"github.com/iwind/TeaGo/Tea"
    11  	_ "github.com/iwind/TeaGo/bootstrap"
    12  	"github.com/iwind/TeaGo/logs"
    13  	"github.com/iwind/TeaGo/types"
    14  	"io"
    15  	"os"
    16  	"testing"
    17  )
    18  
    19  func TestFS_OpenFileWriter(t *testing.T) {
    20  	fs, openErr := bfs.OpenFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
    21  	if openErr != nil {
    22  		t.Fatal(openErr)
    23  	}
    24  	defer func() {
    25  		_ = fs.Close()
    26  	}()
    27  
    28  	{
    29  		writer, err := fs.OpenFileWriter(bfs.Hash("123456"), -1, false)
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  
    34  		err = writer.WriteMeta(200, fasttime.Now().Unix()+3600, -1)
    35  		if err != nil {
    36  			t.Fatal(err)
    37  		}
    38  
    39  		_, err = writer.WriteBody([]byte("Hello, World"))
    40  		if err != nil {
    41  			t.Fatal(err)
    42  		}
    43  
    44  		err = writer.Close()
    45  		if err != nil {
    46  			t.Fatal(err)
    47  		}
    48  	}
    49  
    50  	{
    51  		writer, err := fs.OpenFileWriter(bfs.Hash("654321"), 100, true)
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  
    56  		_, err = writer.WriteBody([]byte("Hello, World"))
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  	}
    61  }
    62  
    63  func TestFS_OpenFileReader(t *testing.T) {
    64  	fs, openErr := bfs.OpenFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
    65  	if openErr != nil {
    66  		t.Fatal(openErr)
    67  	}
    68  	defer func() {
    69  		_ = fs.Close()
    70  	}()
    71  
    72  	reader, err := fs.OpenFileReader(bfs.Hash("123456"), false)
    73  	if err != nil {
    74  		if bfs.IsNotExist(err) {
    75  			t.Log(err)
    76  			return
    77  		}
    78  		t.Fatal(err)
    79  	}
    80  	data, err := io.ReadAll(reader)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	t.Log(string(data))
    85  	logs.PrintAsJSON(reader.FileHeader(), t)
    86  }
    87  
    88  func TestFS_ExistFile(t *testing.T) {
    89  	fs, openErr := bfs.OpenFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
    90  	if openErr != nil {
    91  		t.Fatal(openErr)
    92  	}
    93  	defer func() {
    94  		_ = fs.Close()
    95  	}()
    96  
    97  	exist, err := fs.ExistFile(bfs.Hash("123456"))
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	t.Log("exist:", exist)
   102  }
   103  
   104  func TestFS_RemoveFile(t *testing.T) {
   105  	fs, openErr := bfs.OpenFS(Tea.Root+"/data/bfs/test", bfs.DefaultFSOptions)
   106  	if openErr != nil {
   107  		t.Fatal(openErr)
   108  	}
   109  	defer func() {
   110  		_ = fs.Close()
   111  	}()
   112  
   113  	var hash = bfs.Hash("123456")
   114  	err := fs.RemoveFile(hash)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	exist, err := fs.ExistFile(bfs.Hash("123456"))
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	t.Log("exist:", exist)
   124  }
   125  
   126  func TestFS_OpenFileWriter_Close(t *testing.T) {
   127  	if !testutils.IsSingleTesting() {
   128  		return
   129  	}
   130  
   131  	fs, openErr := bfs.OpenFS(Tea.Root+"/data/bfs/test", &bfs.FSOptions{
   132  		MaxOpenFiles: 99,
   133  	})
   134  	if openErr != nil {
   135  		t.Fatal(openErr)
   136  	}
   137  	defer func() {
   138  		_ = fs.Close()
   139  	}()
   140  
   141  	var count = 2
   142  	if testutils.IsSingleTesting() {
   143  		count = 100
   144  	}
   145  
   146  	for i := 0; i < count; i++ {
   147  		//t.Log("open", i)
   148  		writer, err := fs.OpenFileWriter(bfs.Hash(types.String(i)), -1, false)
   149  		if err != nil {
   150  			t.Fatal(err)
   151  		}
   152  		_ = writer.Close()
   153  	}
   154  
   155  	t.Log(len(fs.TestBMap()), "block files, pid:", os.Getpid())
   156  
   157  	var p = func() {
   158  		var bNames []string
   159  		fs.TestBList().Range(func(item *linkedlist.Item[string]) (goNext bool) {
   160  			bNames = append(bNames, item.Value)
   161  			return true
   162  		})
   163  
   164  		if len(bNames) != len(fs.TestBMap()) {
   165  			t.Fatal("len(bNames)!=len(bMap)")
   166  		}
   167  
   168  		if len(bNames) < 10 {
   169  			t.Log("["+types.String(len(bNames))+"]", bNames)
   170  		} else {
   171  			t.Log("["+types.String(len(bNames))+"]", bNames[:10], "...")
   172  		}
   173  	}
   174  
   175  	p()
   176  
   177  	{
   178  		writer, err := fs.OpenFileWriter(bfs.Hash(types.String(10)), -1, false)
   179  		if err != nil {
   180  			t.Fatal(err)
   181  		}
   182  		_ = writer.Close()
   183  	}
   184  
   185  	p()
   186  
   187  	// testing closing
   188  	for i := 0; i < 3; i++ {
   189  		writer, err := fs.OpenFileWriter(bfs.Hash(types.String(0)), -1, false)
   190  		if err != nil {
   191  			t.Fatal(err)
   192  		}
   193  		_ = writer.Close()
   194  	}
   195  
   196  	p()
   197  }