75 lines
1.4 KiB
Go
Raw Normal View History

2025-10-03 14:28:14 +02:00
package codex
2025-10-07 15:15:21 +02:00
import (
"bytes"
2025-10-07 15:15:21 +02:00
"testing"
)
2025-10-03 14:28:14 +02:00
type codexNodeTestOption func(*codexNodeTestOptions)
type codexNodeTestOptions struct {
noStart bool
blockRetries int
}
func withNoStart() codexNodeTestOption {
return func(o *codexNodeTestOptions) { o.noStart = true }
}
func withBlockRetries(n int) codexNodeTestOption {
return func(o *codexNodeTestOptions) { o.blockRetries = n }
}
func newCodexNode(t *testing.T, opts ...codexNodeTestOption) *CodexNode {
o := codexNodeTestOptions{
blockRetries: 3000,
}
for _, opt := range opts {
opt(&o)
}
2025-10-13 13:14:42 +02:00
node, err := New(Config{
2025-10-07 15:15:21 +02:00
DataDir: t.TempDir(),
LogFormat: LogFormatNoColors,
MetricsEnabled: false,
BlockRetries: o.blockRetries,
2025-10-03 14:28:14 +02:00
})
if err != nil {
t.Fatalf("Failed to create Codex node: %v", err)
}
if !o.noStart {
2025-10-08 06:40:36 +02:00
err = node.Start()
if err != nil {
t.Fatalf("Failed to start Codex node: %v", err)
}
2025-10-03 14:28:14 +02:00
}
t.Cleanup(func() {
if !o.noStart {
2025-10-08 06:40:36 +02:00
if err := node.Stop(); err != nil {
t.Logf("cleanup codex: %v", err)
}
2025-10-03 14:28:14 +02:00
}
if err := node.Destroy(); err != nil {
t.Logf("cleanup codex: %v", err)
}
})
return node
}
func uploadHelper(t *testing.T, codex *CodexNode) (string, int) {
t.Helper()
buf := bytes.NewBuffer([]byte("Hello World!"))
len := buf.Len()
cid, err := codex.UploadReader(UploadOptions{Filepath: "hello.txt"}, buf)
if err != nil {
t.Fatalf("Error happened during upload: %v\n", err)
}
return cid, len
}