github.com/auxten/ginkgo@v0.0.0-20220130172820-7d98ad59d232/download/block_test.go (about)

     1  package download
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/auxten/ginkgo/fileserv"
    12  	"github.com/auxten/ginkgo/seed"
    13  	"github.com/labstack/echo/v4"
    14  	. "github.com/smartystreets/goconvey/convey"
    15  )
    16  
    17  func TestBlockDownloader_DownBlock(t *testing.T) {
    18  	e := echo.New()
    19  	e.GET("/api/seed", fileserv.SeedApi("../test"))
    20  	e.GET("/api/block", fileserv.BlockApi("../test"))
    21  	go e.Start("127.0.0.1:0")
    22  	defer e.Close()
    23  	time.Sleep(time.Second)
    24  	addr := e.ListenerAddr()
    25  	bd := &BlockDownloader{}
    26  	sd, _ := bd.GetSeed(addr.String(), "./", 10)
    27  
    28  	Convey("block download 0 1", t, func() {
    29  		r, err := bd.DownBlock(sd, addr.String(), 0, 1)
    30  		So(err, ShouldBeNil)
    31  		defer r.Close()
    32  		bytes, err := io.ReadAll(r)
    33  		So(err, ShouldBeNil)
    34  		So(string(bytes), ShouldResemble, "1111111112")
    35  	})
    36  
    37  	Convey("block download 0 -1", t, func() {
    38  		r, err := bd.DownBlock(sd, addr.String(), 0, -1)
    39  		So(err, ShouldBeNil)
    40  		defer r.Close()
    41  		bytes, err := io.ReadAll(r)
    42  		So(err, ShouldBeNil)
    43  		So(string(bytes), ShouldResemble, "1111111112222222222222222222223333333333333")
    44  	})
    45  
    46  	Convey("block download 1 3", t, func() {
    47  		r, err := bd.DownBlock(sd, addr.String(), 1, 3)
    48  		So(err, ShouldBeNil)
    49  		defer r.Close()
    50  		bytes, err := io.ReadAll(r)
    51  		So(err, ShouldBeNil)
    52  		So(string(bytes), ShouldResemble, "222222222222222222223333333333")
    53  	})
    54  }
    55  
    56  func TestJoin(t *testing.T) {
    57  	var (
    58  		hostPath = new(seed.HostPath)
    59  		err      error
    60  		dir      = "./dir1"
    61  	)
    62  
    63  	e1 := echo.New()
    64  	e1.GET("/api/seed", fileserv.SeedApi("../test"))
    65  	e1.POST("/api/join", fileserv.JoinApi())
    66  	go e1.Start("127.0.0.1:0")
    67  	defer e1.Close()
    68  	e2 := echo.New()
    69  	e2.POST("/api/join", func(c echo.Context) error {
    70  		return c.Bind(hostPath)
    71  	})
    72  	go e2.Start("127.0.0.1:0")
    73  	defer e2.Close()
    74  	time.Sleep(time.Second)
    75  	addr1 := e1.ListenerAddr()
    76  	host1, _ := seed.ParseHost(addr1.String())
    77  	addr2 := e2.ListenerAddr()
    78  	host2, _ := seed.ParseHost(addr2.String())
    79  
    80  	bd := &BlockDownloader{MyHost: host1}
    81  	// trigger make seed
    82  	bd.GetSeed(addr1.String(), dir, 10)
    83  
    84  	bd.BroadcastJoin(host2, dir, addr1.String())
    85  
    86  	sd, _ := bd.GetSeed(addr1.String(), dir, 10)
    87  	if len(sd.Hosts) != 2 {
    88  		t.Errorf("expect 2 host, got %s", sd.Hosts)
    89  	}
    90  	if hostPath.Host != host1.String() {
    91  		t.Errorf("expect host %s, got %s", host1, hostPath.Host)
    92  	}
    93  	if hostPath.Path != dir {
    94  		t.Errorf("expect path %s, got %s", dir, hostPath.Path)
    95  	}
    96  
    97  	err = bd.BroadcastJoin(host2, "./notExist", addr1.String())
    98  	if err.Error() != "404 Not Found" {
    99  		t.Errorf("expect error got %s", err.Error())
   100  	}
   101  }
   102  
   103  func TestBlockDownloader_WriteBlock(t *testing.T) {
   104  	e := echo.New()
   105  	e.GET("/api/seed", fileserv.SeedApi("../test"))
   106  	e.GET("/api/block", fileserv.BlockApi("../test"))
   107  	go e.Start("127.0.0.1:0")
   108  	defer e.Close()
   109  	time.Sleep(time.Second)
   110  	addr := e.ListenerAddr()
   111  	bd := &BlockDownloader{}
   112  
   113  	Convey("block write 0 1", t, func() {
   114  		dir, err := ioutil.TempDir("", "blockWrite")
   115  		So(err, ShouldBeNil)
   116  		defer os.RemoveAll(dir) // clean up
   117  		sd, err := bd.GetSeed(addr.String(), "./", 10)
   118  		So(err, ShouldBeNil)
   119  		err = sd.Localize("./", dir)
   120  		So(err, ShouldBeNil)
   121  		r, err := bd.DownBlock(sd, addr.String(), 0, 1)
   122  		So(err, ShouldBeNil)
   123  		defer r.Close()
   124  		wrote, err := bd.WriteBlock(sd, r, 0, 1)
   125  		So(err, ShouldBeNil)
   126  		So(wrote, ShouldBeGreaterThan, 0)
   127  		bytes, err := os.ReadFile(filepath.Join(dir, "dir1/file11"))
   128  		So(err, ShouldBeNil)
   129  		So(string(bytes), ShouldResemble, "111111111")
   130  		bytes, err = os.ReadFile(filepath.Join(dir, "dir1/file12"))
   131  		So(err, ShouldBeNil)
   132  		So(string(bytes), ShouldResemble, "2")
   133  		So(sd.Blocks[0].Done, ShouldBeTrue)
   134  		So(sd.Blocks[1].Done, ShouldBeFalse)
   135  	})
   136  
   137  	Convey("block write 0 -1", t, func() {
   138  		dir, err := ioutil.TempDir("", "blockWrite")
   139  		So(err, ShouldBeNil)
   140  		defer os.RemoveAll(dir) // clean up
   141  		sd, err := bd.GetSeed(addr.String(), "./", 10)
   142  		So(err, ShouldBeNil)
   143  		err = sd.Localize("./", dir)
   144  		So(err, ShouldBeNil)
   145  		r, err := bd.DownBlock(sd, addr.String(), 0, -1)
   146  		So(err, ShouldBeNil)
   147  		defer r.Close()
   148  		wrote, err := bd.WriteBlock(sd, r, 0, -1)
   149  		So(err, ShouldBeNil)
   150  		So(wrote, ShouldBeGreaterThan, 0)
   151  		bytes, err := os.ReadFile(filepath.Join(dir, "dir1/file11"))
   152  		So(err, ShouldBeNil)
   153  		So(string(bytes), ShouldResemble, "111111111")
   154  		bytes, err = os.ReadFile(filepath.Join(dir, "dir1/file12"))
   155  		So(err, ShouldBeNil)
   156  		So(string(bytes), ShouldResemble, "222222222222222222222")
   157  		bytes, err = os.ReadFile(filepath.Join(dir, "dir1/file13"))
   158  		So(err, ShouldBeNil)
   159  		So(string(bytes), ShouldResemble, "3333333333333")
   160  		for _, b := range sd.Blocks {
   161  			So(b.Done, ShouldBeTrue)
   162  		}
   163  	})
   164  
   165  	Convey("block write 1 3", t, func() {
   166  		dir, err := ioutil.TempDir("", "blockWrite")
   167  		So(err, ShouldBeNil)
   168  		defer os.RemoveAll(dir) // clean up
   169  		sd, err := bd.GetSeed(addr.String(), "./", 10)
   170  		So(err, ShouldBeNil)
   171  		err = sd.Localize("./", dir)
   172  		So(err, ShouldBeNil)
   173  		r, err := bd.DownBlock(sd, addr.String(), 1, 3)
   174  		So(err, ShouldBeNil)
   175  		defer r.Close()
   176  		wrote, err := bd.WriteBlock(sd, r, 1, 3)
   177  		So(wrote, ShouldBeGreaterThan, 0)
   178  		So(err, ShouldBeNil)
   179  		bytes, err := os.ReadFile(filepath.Join(dir, "dir1/file11"))
   180  		So(err.Error(), ShouldContainSubstring, "no such file or directory")
   181  		bytes, err = os.ReadFile(filepath.Join(dir, "dir1/file12"))
   182  		So(err, ShouldBeNil)
   183  		So(string(bytes), ShouldResemble, "\x0022222222222222222222")
   184  		bytes, err = os.ReadFile(filepath.Join(dir, "dir1/file13"))
   185  		So(err, ShouldBeNil)
   186  		So(string(bytes), ShouldResemble, "3333333333")
   187  		for i, b := range sd.Blocks {
   188  			if i >= 1 && i <= 3 {
   189  				So(b.Done, ShouldBeTrue)
   190  			} else {
   191  				So(b.Done, ShouldBeFalse)
   192  			}
   193  		}
   194  	})
   195  
   196  }