示例#1
0
func Test001ArchiverFileMgr(t *testing.T) {

	cv.Convey("given an archiver, messages that roll to different dates should be stored in distinct files", t, func() {
		tmp, err := ioutil.TempDir("", "test-archiver-filemgr")
		panicOn(err)
		defer os.RemoveAll(tmp)
		fm := NewFileMgr(&ArchiverConfig{WriteDir: tmp})

		tm1, err := time.Parse(time.RFC3339, "2016-01-01T00:00:00Z")
		panicOn(err)
		tm2, err := time.Parse(time.RFC3339, "2016-01-02T00:00:00Z")
		panicOn(err)

		streamName := "test"
		data1 := []byte("data1")
		data2 := []byte("data2")

		frame1, err := ts.NewFrame(tm1, ts.EvUtf8, 0, 0, data1)
		panicOn(err)
		by1, err := frame1.Marshal(nil)
		file1, err := fm.Store(tm1, streamName, by1)
		panicOn(err)

		frame2, err := ts.NewFrame(tm2, ts.EvUtf8, 0, 0, data2)
		panicOn(err)
		by2, err := frame2.Marshal(nil)
		file2, err := fm.Store(tm2, streamName, by2)
		panicOn(err)

		p("For inspection, we stored into file1 = '%v'\n", file1.Path)
		q("file2 = '%#v'\n", file2)
		cv.So(strings.Contains(file1.Path, "2016/01/01"), cv.ShouldBeTrue)
		cv.So(strings.Contains(file2.Path, "2016/01/02"), cv.ShouldBeTrue)

		cv.So(FileExists(file1.Path), cv.ShouldBeTrue)
		cv.So(FileExists(file2.Path), cv.ShouldBeTrue)

		by, err := ioutil.ReadFile(file1.Path)
		panicOn(err)

		var fr ts.Frame
		_, err = fr.Unmarshal(by, true)
		p("fr = %#v", fr)
		panicOn(err)

		cv.So(fr.GetEvtnum(), cv.ShouldEqual, ts.EvUtf8)
		cv.So(fr.GetUlen(), cv.ShouldEqual, len(frame1.Data)+1)

		p("Given that we've written an event to its file, we should be able to recover what we've written")
		cv.So(string(fr.Data), cv.ShouldResemble, string(data1))
	})
}