1 Star 0 Fork 0

蔡风华/fisher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
recorder_test.go 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
蔡风华 提交于 2023-06-06 14:35 . 1.更新依赖。
package fisher
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"testing"
"time"
"gitee.com/cfh008/baseutils/extendutil"
"gitee.com/cfh008/ioutils"
"github.com/stretchr/testify/assert"
)
func TestRecordData(t *testing.T) {
asserter := assert.New(t)
// 临时创建一个文件用于验证文件写入
file, err := ioutil.TempFile("", "recorder-*.jl")
asserter.Nil(err)
name := file.Name()
file.Close()
// 确保程序结束时删除临时文件
defer os.Remove(file.Name())
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
var writer *ioutils.FileWriter = nil
srcFile, err := os.Create(name)
asserter.Nil(err)
writer = ioutils.NewFileWriter(srcFile)
StartRecoder(ctx, writer)
toWriteFirst := []byte("The data to write!")
toWriteSecond := make(map[string]interface{})
toWriteSecond["a"] = "b"
toWriteSecond["what"] = 32
toWriteSecondData, err := json.Marshal(toWriteSecond)
asserter.Nil(err)
toWriteThird := "The data to write!"
toWriteThirdData := []byte(toWriteThird)
expected := extendutil.JoinBytes([]byte("\n"), toWriteFirst, toWriteSecondData, toWriteThirdData)
expected = extendutil.JoinBytes([]byte(""), expected, []byte("\n"))
err = RecordData(toWriteFirst)
asserter.Nil(err)
err = RecordData(toWriteSecond)
asserter.Nil(err)
err = RecordData(toWriteThird)
asserter.Nil(err)
time.Sleep(3 * time.Second)
data, err := ioutil.ReadFile(file.Name())
asserter.Nil(err)
asserter.Equal(expected, data)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/cfh008/fisher.git
git@gitee.com:cfh008/fisher.git
cfh008
fisher
fisher
master

搜索帮助