github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/writer_partial_file_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package caches_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/caches"
     7  	fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
     8  	"github.com/iwind/TeaGo/types"
     9  	"os"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestPartialFileWriter_Write(t *testing.T) {
    15  	var path = "/tmp/test_partial.cache"
    16  	_ = os.Remove(path)
    17  
    18  	var reader = func() {
    19  		data, err := fsutils.ReadFile(path)
    20  		if err != nil {
    21  			t.Fatal(err)
    22  		}
    23  		t.Log("["+types.String(len(data))+"]", string(data))
    24  	}
    25  
    26  	fp, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	var ranges = caches.NewPartialRanges(0)
    31  	var writer = caches.NewPartialFileWriter(fp, "test", time.Now().Unix()+86500, -1, -1, true, true, 0, ranges, func() {
    32  		t.Log("end")
    33  	})
    34  	_, err = writer.WriteHeader([]byte("header"))
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	// 移动位置
    40  	err = writer.WriteAt(100, []byte("HELLO"))
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	err = writer.Close()
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	reader()
    51  }