284 lines
10 KiB
Zig
284 lines
10 KiB
Zig
//! Comprehensive tests for upload management
|
|
//! Tests initialization, piece reading, rate limiting, concurrent requests,
|
|
//! upload statistics, and error handling
|
|
|
|
const std = @import("std");
|
|
const testing = std.testing;
|
|
const files = @import("files");
|
|
const core = @import("core");
|
|
const Error = core.errors.Error;
|
|
|
|
test "upload: initialization with valid parameters" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(
|
|
1024 * 1024, // 1MB max bandwidth
|
|
1024, // piece size
|
|
allocator,
|
|
);
|
|
defer manager.deinit();
|
|
|
|
try testing.expectEqual(@as(u64, 1024 * 1024), manager.state.max_bandwidth);
|
|
try testing.expectEqual(@as(u64, 1024), manager.file_chunker.piece_size);
|
|
}
|
|
|
|
test "upload: add available piece" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0xAB} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
|
|
try testing.expectEqual(@as(usize, 1), manager.available_pieces.count());
|
|
const piece = manager.available_pieces.get(0).?;
|
|
try testing.expectEqual(@as(u32, 0), piece.index);
|
|
}
|
|
|
|
test "upload: remove available piece" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0xCD} ** 32;
|
|
try manager.addAvailablePiece(5, hash);
|
|
try testing.expectEqual(@as(usize, 1), manager.available_pieces.count());
|
|
|
|
manager.removeAvailablePiece(5);
|
|
try testing.expectEqual(@as(usize, 0), manager.available_pieces.count());
|
|
}
|
|
|
|
test "upload: handle upload request for available piece" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0xEF} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
|
|
const accepted = try manager.handleUploadRequest(0, 0, 512, "peer1", .normal);
|
|
try testing.expect(accepted);
|
|
try testing.expectEqual(@as(usize, 1), manager.pending_requests.items.len);
|
|
}
|
|
|
|
test "upload: reject upload request for unavailable piece" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const accepted = try manager.handleUploadRequest(99, 0, 512, "peer1", .normal);
|
|
try testing.expect(!accepted);
|
|
}
|
|
|
|
test "upload: bandwidth limiting" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(1000, 1024, allocator); // Small bandwidth limit
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0x01} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
|
|
// Request large upload that exceeds bandwidth
|
|
const accepted = try manager.handleUploadRequest(0, 0, 2000, "peer1", .normal);
|
|
try testing.expect(!accepted); // Should be rejected due to bandwidth
|
|
}
|
|
|
|
test "upload: process upload requests" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(10 * 1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0x02} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
try manager.addAvailablePiece(1, hash);
|
|
|
|
// Create multiple upload requests
|
|
_ = try manager.handleUploadRequest(0, 0, 512, "peer1", .normal);
|
|
_ = try manager.handleUploadRequest(1, 0, 512, "peer2", .normal);
|
|
|
|
// Process with max concurrent = 2
|
|
try manager.processRequests(2);
|
|
|
|
try testing.expectEqual(@as(usize, 2), manager.active_uploads.count());
|
|
try testing.expectEqual(@as(usize, 0), manager.pending_requests.items.len);
|
|
}
|
|
|
|
test "upload: request priority sorting" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(10 * 1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0x03} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
try manager.addAvailablePiece(1, hash);
|
|
try manager.addAvailablePiece(2, hash);
|
|
|
|
// Add requests with different priorities
|
|
_ = try manager.handleUploadRequest(0, 0, 512, "peer1", .low);
|
|
_ = try manager.handleUploadRequest(1, 0, 512, "peer2", .urgent);
|
|
_ = try manager.handleUploadRequest(2, 0, 512, "peer3", .normal);
|
|
|
|
// Process one at a time to check priority order
|
|
try manager.processRequests(1);
|
|
|
|
// First active should be urgent priority (piece index 1)
|
|
try testing.expect(manager.active_uploads.contains(1));
|
|
}
|
|
|
|
test "upload: peer upload limits" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(10 * 1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0x04} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
try manager.addAvailablePiece(1, hash);
|
|
|
|
// Set peer limit to 1 concurrent upload
|
|
try manager.setPeerLimit("peer1", 1, 1024 * 1024);
|
|
|
|
// First request should succeed
|
|
const accepted1 = try manager.handleUploadRequest(0, 0, 512, "peer1", .normal);
|
|
try testing.expect(accepted1);
|
|
|
|
// Process to start upload
|
|
try manager.processRequests(5);
|
|
|
|
// Second request for same peer should be rejected
|
|
const accepted2 = try manager.handleUploadRequest(1, 0, 512, "peer1", .normal);
|
|
try testing.expect(!accepted2);
|
|
}
|
|
|
|
test "upload: cancel upload" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(10 * 1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0x05} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
|
|
_ = try manager.handleUploadRequest(0, 0, 512, "peer1", .normal);
|
|
try manager.processRequests(5);
|
|
|
|
try testing.expectEqual(@as(usize, 1), manager.active_uploads.count());
|
|
|
|
try manager.cancelUpload(0, "User cancelled");
|
|
|
|
try testing.expectEqual(@as(usize, 0), manager.active_uploads.count());
|
|
try testing.expectEqual(@as(u32, 1), manager.state.failed_uploads.load(.monotonic));
|
|
}
|
|
|
|
test "upload: send piece data updates statistics" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(10 * 1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0x06} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
|
|
_ = try manager.handleUploadRequest(0, 0, 512, "peer1", .normal);
|
|
try manager.processRequests(5);
|
|
|
|
const test_data = [_]u8{'X'} ** 512;
|
|
try manager.sendPieceData(0, &test_data);
|
|
|
|
const stats = manager.getStats();
|
|
try testing.expectEqual(@as(u64, 512), stats.state.total_uploaded);
|
|
try testing.expectEqual(@as(u32, 1), stats.state.completed_uploads);
|
|
}
|
|
|
|
test "upload: bandwidth utilization calculation" {
|
|
const allocator = testing.allocator;
|
|
const max_bandwidth: u64 = 1024 * 1024;
|
|
var manager = try files.upload.UploadManager.init(max_bandwidth, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
// Add some uploaded bytes
|
|
manager.state.addUploadedBytes(512 * 1024);
|
|
|
|
const utilization = manager.state.getBandwidthUtilization();
|
|
try testing.expect(utilization >= 0.0 and utilization <= 1.0);
|
|
}
|
|
|
|
test "upload: bandwidth reset" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
manager.state.addUploadedBytes(1024);
|
|
try testing.expect(manager.state.getCurrentBandwidth() > 0);
|
|
|
|
manager.resetBandwidth();
|
|
try testing.expectEqual(@as(u64, 0), manager.state.getCurrentBandwidth());
|
|
}
|
|
|
|
test "upload: set bandwidth limit" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const new_limit: u64 = 2 * 1024 * 1024;
|
|
manager.setBandwidthLimit(new_limit);
|
|
|
|
try testing.expectEqual(new_limit, manager.state.max_bandwidth);
|
|
try testing.expectEqual(new_limit, manager.limiter.max_bandwidth);
|
|
}
|
|
|
|
test "upload: get piece statistics" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(10 * 1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0x07} ** 32;
|
|
try manager.addAvailablePiece(3, hash);
|
|
|
|
const stats = manager.getPieceStats(3);
|
|
try testing.expect(stats != null);
|
|
try testing.expectEqual(@as(u32, 3), stats.?.index);
|
|
try testing.expect(stats.?.is_available);
|
|
try testing.expectEqual(@as(u32, 0), stats.?.upload_count);
|
|
}
|
|
|
|
test "upload: read piece data with boundaries" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(10 * 1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
// Create test file
|
|
const test_file = "test_upload_piece.bin";
|
|
const test_data = "This is test data for upload piece reading";
|
|
|
|
const file = try std.fs.cwd().createFile(test_file, .{});
|
|
_ = try file.write(test_data);
|
|
file.close();
|
|
defer std.fs.cwd().deleteFile(test_file) catch {};
|
|
|
|
try manager.setFilePath(test_file, test_data.len);
|
|
|
|
const hash = [_]u8{0x08} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
|
|
// Try to read piece - should fail because it exceeds file bounds
|
|
const result = manager.readPieceData(0, 0, @intCast(test_data.len + 100));
|
|
try testing.expectError(Error.InvalidData, result);
|
|
}
|
|
|
|
test "upload: request expiration cleanup" {
|
|
const allocator = testing.allocator;
|
|
var manager = try files.upload.UploadManager.init(10 * 1024 * 1024, 1024, allocator);
|
|
defer manager.deinit();
|
|
|
|
const hash = [_]u8{0x09} ** 32;
|
|
try manager.addAvailablePiece(0, hash);
|
|
|
|
// Create old request
|
|
var request = files.upload.UploadRequest.create(0, 0, 512, "peer1", .normal);
|
|
request.requested_at = std.time.timestamp() - 100; // 100 seconds ago
|
|
try manager.pending_requests.append(allocator, request);
|
|
|
|
manager.request_timeout = 30; // 30 second timeout
|
|
try manager.processRequests(5);
|
|
|
|
// Expired request should be cleaned up
|
|
try testing.expectEqual(@as(usize, 0), manager.pending_requests.items.len);
|
|
}
|