代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。