github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/bfs/file_writer_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  	"bytes"
     7  	"github.com/TeaOSLab/EdgeNode/internal/utils/bfs"
     8  	"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
     9  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
    10  	"github.com/iwind/TeaGo/logs"
    11  	"net/http"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestNewFileWriter(t *testing.T) {
    17  	bFile, err := bfs.OpenBlocksFile("testdata/test.b", bfs.DefaultBlockFileOptions)
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  
    22  	defer func() {
    23  		if !testutils.IsSingleTesting() {
    24  			_ = bFile.RemoveAll()
    25  		} else {
    26  			_ = bFile.Close()
    27  		}
    28  	}()
    29  
    30  	writer, err := bFile.OpenFileWriter(bfs.Hash("123456"), -1, false)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	err = writer.WriteMeta(http.StatusOK, fasttime.Now().Unix()+3600, -1)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	_, err = writer.WriteHeader([]byte("Content-Type: text/html; charset=utf-8"))
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	for i := 0; i < 3; i++ {
    46  		n, writeErr := writer.WriteBody([]byte("Hello,World"))
    47  		if writeErr != nil {
    48  			t.Fatal(writeErr)
    49  		}
    50  
    51  		t.Log("wrote:", n, "bytes")
    52  	}
    53  
    54  	err = writer.Close()
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  }
    59  
    60  func TestNewFileWriter_LargeFile(t *testing.T) {
    61  	bFile, err := bfs.OpenBlocksFile("testdata/test.b", bfs.DefaultBlockFileOptions)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	defer func() {
    67  		if !testutils.IsSingleTesting() {
    68  			_ = bFile.RemoveAll()
    69  		} else {
    70  			_ = bFile.Close()
    71  		}
    72  	}()
    73  
    74  	writer, err := bFile.OpenFileWriter(bfs.Hash("123456@LARGE"), -1, false)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	err = writer.WriteMeta(http.StatusOK, fasttime.Now().Unix()+86400, -1)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	var countBlocks = 1 << 10
    85  	if !testutils.IsSingleTesting() {
    86  		countBlocks = 2
    87  	}
    88  
    89  	var data = bytes.Repeat([]byte{'A'}, 16<<10)
    90  
    91  	var before = time.Now()
    92  	for i := 0; i < countBlocks; i++ {
    93  		_, err = writer.WriteBody(data)
    94  		if err != nil {
    95  			t.Fatal(err)
    96  		}
    97  	}
    98  
    99  	err = writer.Close()
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	logs.Println("cost:", time.Since(before).Seconds()*1000, "ms")
   105  }
   106  
   107  func TestFileWriter_WriteBodyAt(t *testing.T) {
   108  	bFile, err := bfs.OpenBlocksFile("testdata/test.b", bfs.DefaultBlockFileOptions)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	defer func() {
   114  		if !testutils.IsSingleTesting() {
   115  			_ = bFile.RemoveAll()
   116  		} else {
   117  			_ = bFile.Close()
   118  		}
   119  	}()
   120  
   121  	writer, err := bFile.OpenFileWriter(bfs.Hash("123456"), 1<<20, true)
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  
   126  	{
   127  		n, writeErr := writer.WriteBodyAt([]byte("Hello,World"), 1024)
   128  		if writeErr != nil {
   129  			t.Fatal(writeErr)
   130  		}
   131  
   132  		t.Log("wrote:", n, "bytes")
   133  	}
   134  }