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

     1  package caches
     2  
     3  import (
     4  	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
     5  	"github.com/iwind/TeaGo/Tea"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  func TestFileReader(t *testing.T) {
    11  	var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{
    12  		Id:   1,
    13  		IsOn: true,
    14  		Options: map[string]interface{}{
    15  			"dir": Tea.Root + "/caches",
    16  		},
    17  	})
    18  
    19  	defer storage.Stop()
    20  
    21  	err := storage.Init()
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	_, path, _ := storage.keyPath("my-key")
    27  
    28  	fp, err := os.Open(path)
    29  	if err != nil {
    30  		if os.IsNotExist(err) {
    31  			t.Log("file '" + path + "' not exists")
    32  			return
    33  		}
    34  		t.Fatal(err)
    35  	}
    36  	defer func() {
    37  		_ = fp.Close()
    38  	}()
    39  	reader := NewFileReader(fp)
    40  	err = reader.Init()
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	t.Log(reader.Status())
    46  
    47  	buf := make([]byte, 10)
    48  	err = reader.ReadHeader(buf, func(n int) (goNext bool, err error) {
    49  		t.Log("header:", string(buf[:n]))
    50  		return true, nil
    51  	})
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	err = reader.ReadBody(buf, func(n int) (goNext bool, err error) {
    57  		t.Log("body:", string(buf[:n]))
    58  		return true, nil
    59  	})
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  }
    64  
    65  func TestFileReader_ReadHeader(t *testing.T) {
    66  	var path = "/Users/WorkSpace/EdgeProject/EdgeCache/p43/12/6b/126bbed90fc80f2bdfb19558948b0d49.cache"
    67  	fp, err := os.Open(path)
    68  	if err != nil {
    69  		if os.IsNotExist(err) {
    70  			t.Log("'" + path + "' not exists")
    71  			return
    72  		}
    73  		t.Fatal(err)
    74  	}
    75  	defer func() {
    76  		_ = fp.Close()
    77  	}()
    78  	var reader = NewFileReader(fp)
    79  	err = reader.Init()
    80  	if err != nil {
    81  		if os.IsNotExist(err) {
    82  			t.Log("file '" + path + "' not exists")
    83  			return
    84  		}
    85  
    86  		t.Fatal(err)
    87  	}
    88  	var buf = make([]byte, 16*1024)
    89  	err = reader.ReadHeader(buf, func(n int) (goNext bool, err error) {
    90  		t.Log("header:", string(buf[:n]))
    91  		return
    92  	})
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  }
    97  
    98  func TestFileReader_Range(t *testing.T) {
    99  	var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{
   100  		Id:   1,
   101  		IsOn: true,
   102  		Options: map[string]interface{}{
   103  			"dir": Tea.Root + "/caches",
   104  		},
   105  	})
   106  
   107  	defer storage.Stop()
   108  
   109  	err := storage.Init()
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	/**writer, err := storage.Open("my-number", time.Now().Unix()+30*86400, 200, 6, 10)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	_, err = writer.Write([]byte("Header"))
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	_, err = writer.Write([]byte("0123456789"))
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	_ = writer.Close()**/
   127  
   128  	_, path, _ := storage.keyPath("my-number")
   129  
   130  	fp, err := os.Open(path)
   131  	if err != nil {
   132  		if os.IsNotExist(err) {
   133  			t.Log("'" + path + "' not exists")
   134  			return
   135  		}
   136  		t.Fatal(err)
   137  	}
   138  	defer func() {
   139  		_ = fp.Close()
   140  	}()
   141  	reader := NewFileReader(fp)
   142  	err = reader.Init()
   143  	if err != nil {
   144  		t.Fatal(err)
   145  	}
   146  
   147  	buf := make([]byte, 6)
   148  	{
   149  		err = reader.ReadBodyRange(buf, 0, 0, func(n int) (goNext bool, err error) {
   150  			t.Log("[0, 0]", "body:", string(buf[:n]))
   151  			return true, nil
   152  		})
   153  		if err != nil {
   154  			t.Fatal(err)
   155  		}
   156  	}
   157  	{
   158  		err = reader.ReadBodyRange(buf, 7, 7, func(n int) (goNext bool, err error) {
   159  			t.Log("[7, 7]", "body:", string(buf[:n]))
   160  			return true, nil
   161  		})
   162  		if err != nil {
   163  			t.Fatal(err)
   164  		}
   165  	}
   166  	{
   167  		err = reader.ReadBodyRange(buf, 0, 10, func(n int) (goNext bool, err error) {
   168  			t.Log("[0, 10]", "body:", string(buf[:n]))
   169  			return true, nil
   170  		})
   171  		if err != nil {
   172  			t.Fatal(err)
   173  		}
   174  	}
   175  	{
   176  		err = reader.ReadBodyRange(buf, 3, 5, func(n int) (goNext bool, err error) {
   177  			t.Log("[3, 5]", "body:", string(buf[:n]))
   178  			return true, nil
   179  		})
   180  		if err != nil {
   181  			t.Fatal(err)
   182  		}
   183  	}
   184  	{
   185  		err = reader.ReadBodyRange(buf, -1, -3, func(n int) (goNext bool, err error) {
   186  			t.Log("[, -3]", "body:", string(buf[:n]))
   187  			return true, nil
   188  		})
   189  		if err != nil {
   190  			t.Fatal(err)
   191  		}
   192  	}
   193  	{
   194  		err = reader.ReadBodyRange(buf, 3, -1, func(n int) (goNext bool, err error) {
   195  			t.Log("[3, ]", "body:", string(buf[:n]))
   196  			return true, nil
   197  		})
   198  		if err != nil {
   199  			t.Fatal(err)
   200  		}
   201  	}
   202  }