notify/file_transfer_config_test.go

105 lines
3.4 KiB
Go
Raw Normal View History

package notify
import (
"context"
"os"
"path/filepath"
"reflect"
"testing"
"time"
)
func TestClientFileTransferConfigDefaults(t *testing.T) {
client := NewClient().(*ClientCommon)
cfg := client.getFileTransferConfig()
if got, want := cfg.ChunkSize, defaultFileChunkSize; got != want {
t.Fatalf("chunk size mismatch: got %d want %d", got, want)
}
if got, want := cfg.AckTimeout, defaultFileAckTimeout; got != want {
t.Fatalf("ack timeout mismatch: got %v want %v", got, want)
}
if got, want := cfg.SendRetry, defaultFileSendRetry; got != want {
t.Fatalf("send retry mismatch: got %d want %d", got, want)
}
if got, want := cfg.ReceiveCompletedLimit, defaultFileReceiveCompletedLimit; got != want {
t.Fatalf("receive completed limit mismatch: got %d want %d", got, want)
}
if got, want := cfg.MonitorCompletedLimit, defaultFileTransferCompletedLimit; got != want {
t.Fatalf("monitor completed limit mismatch: got %d want %d", got, want)
}
}
func TestServerFileTransferConfigNormalization(t *testing.T) {
server := NewServer().(*ServerCommon)
server.setFileTransferConfig(fileTransferConfig{})
cfg := server.getFileTransferConfig()
if got, want := cfg.ChunkSize, defaultFileChunkSize; got != want {
t.Fatalf("normalized chunk size mismatch: got %d want %d", got, want)
}
if got, want := cfg.AckTimeout, defaultFileAckTimeout; got != want {
t.Fatalf("normalized ack timeout mismatch: got %v want %v", got, want)
}
if got, want := cfg.SendRetry, defaultFileSendRetry; got != want {
t.Fatalf("normalized retry mismatch: got %d want %d", got, want)
}
if got, want := cfg.ReceiveCompletedLimit, defaultFileReceiveCompletedLimit; got != want {
t.Fatalf("normalized receive completed limit mismatch: got %d want %d", got, want)
}
if got, want := cfg.MonitorCompletedLimit, defaultFileTransferCompletedLimit; got != want {
t.Fatalf("normalized monitor completed limit mismatch: got %d want %d", got, want)
}
}
func TestClientFileTransferConfigPropagatesRetentionLimits(t *testing.T) {
client := NewClient().(*ClientCommon)
client.setFileTransferConfig(fileTransferConfig{
ChunkSize: 64,
AckTimeout: time.Second,
SendRetry: 2,
ReceiveCompletedLimit: 7,
MonitorCompletedLimit: 9,
})
if got, want := client.getFileReceivePool().completedLimit, 7; got != want {
t.Fatalf("client receive pool completed limit mismatch: got %d want %d", got, want)
}
if got, want := client.getFileTransferState().monitorView().completedLimit, 9; got != want {
t.Fatalf("client transfer monitor completed limit mismatch: got %d want %d", got, want)
}
}
func TestSendFileWithHooksHonorsConfiguredChunkSize(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "payload.bin")
if err := os.WriteFile(path, []byte("abcdefg"), 0o600); err != nil {
t.Fatalf("write temp file failed: %v", err)
}
var chunks []int
err := sendFileWithHooks(context.Background(), path, fileSendHooks{
config: fileTransferConfig{
ChunkSize: 3,
AckTimeout: time.Millisecond,
SendRetry: 1,
},
sendReliable: func(ctx context.Context, env Envelope) error {
if env.Kind == EnvelopeFileChunk {
chunks = append(chunks, len(env.File.Chunk))
}
return nil
},
})
if err != nil {
t.Fatalf("sendFileWithHooks failed: %v", err)
}
if got, want := chunks, []int{3, 3, 1}; !reflect.DeepEqual(got, want) {
t.Fatalf("chunk sizes mismatch: got %v want %v", got, want)
}
}