98 lines
1.8 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-17 08:55:01 +02:00
"context"
2025-10-07 15:15:21 +02:00
"testing"
)
2025-10-03 14:28:14 +02:00
func defaultConfigHelper(t *testing.T) Config {
t.Helper()
return Config{
DataDir: t.TempDir(),
LogFormat: LogFormatNoColors,
MetricsEnabled: false,
BlockRetries: 3000,
LogLevel: "TRACE",
2025-11-10 19:57:34 +11:00
Nat: "none",
}
}
func newCodexNode(t *testing.T, opts ...Config) *CodexNode {
config := defaultConfigHelper(t)
if len(opts) > 0 {
c := opts[0]
if c.BlockRetries > 0 {
config.BlockRetries = c.BlockRetries
}
if c.LogLevel != "" {
config.LogLevel = c.LogLevel
}
if c.LogFile != "" {
config.LogFile = c.LogFile
}
2025-10-27 10:56:32 +01:00
if len(c.BootstrapNodes) != 0 {
config.BootstrapNodes = c.BootstrapNodes
}
if c.DiscoveryPort != 0 {
config.DiscoveryPort = c.DiscoveryPort
}
}
node, err := New(config)
2025-10-03 14:28:14 +02:00
if err != nil {
t.Fatalf("Failed to create Codex node: %v", err)
}
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 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()
2025-10-17 08:55:01 +02:00
cid, err := codex.UploadReader(context.Background(), UploadOptions{Filepath: "hello.txt"}, buf)
if err != nil {
t.Fatalf("Error happened during upload: %v\n", err)
}
return cid, len
}
func uploadBigFileHelper(t *testing.T, codex *CodexNode) (string, int) {
t.Helper()
len := 1024 * 1024 * 50
buf := bytes.NewBuffer(make([]byte, len))
cid, err := codex.UploadReader(context.Background(), UploadOptions{Filepath: "hello.txt"}, buf)
if err != nil {
t.Fatalf("Error happened during upload: %v\n", err)
}
return cid, len
}