521 lines
16 KiB
Zig
521 lines
16 KiB
Zig
//! Comprehensive tests for verification_pool.zig
|
|
//!
|
|
//! Tests cover pool initialization, task queuing, verification processing,
|
|
//! statistics tracking, and edge cases.
|
|
|
|
const std = @import("std");
|
|
const testing = std.testing;
|
|
const files = @import("files");
|
|
const core = @import("core");
|
|
const Error = core.errors.Error;
|
|
const verification_pool = files.verification_pool;
|
|
const VerificationPool = verification_pool.VerificationPool;
|
|
const VerificationPoolConfig = verification_pool.VerificationPoolConfig;
|
|
const VerificationTask = verification_pool.VerificationTask;
|
|
const VerificationResult = verification_pool.VerificationResult;
|
|
const TaskStorage = verification_pool.TaskStorage;
|
|
const hash_mod = files.hash;
|
|
const PieceHash = hash_mod.PieceHash;
|
|
|
|
// ============================================================================
|
|
// Configuration Tests
|
|
// ============================================================================
|
|
|
|
test "VerificationPoolConfig: default values" {
|
|
const config = VerificationPoolConfig{};
|
|
|
|
try testing.expectEqual(@as(u32, 0), config.num_workers);
|
|
try testing.expectEqual(@as(usize, 1024), config.max_pending_tasks);
|
|
try testing.expect(config.use_results_queue);
|
|
try testing.expectEqual(@as(usize, 128), config.num_piece_buffers);
|
|
try testing.expectEqual(@as(usize, 1024 * 1024), config.piece_buffer_size);
|
|
}
|
|
|
|
test "VerificationPoolConfig: custom values" {
|
|
const config = VerificationPoolConfig{
|
|
.num_workers = 4,
|
|
.max_pending_tasks = 256,
|
|
.use_results_queue = false,
|
|
.num_piece_buffers = 64,
|
|
.piece_buffer_size = 512 * 1024,
|
|
};
|
|
|
|
try testing.expectEqual(@as(u32, 4), config.num_workers);
|
|
try testing.expectEqual(@as(usize, 256), config.max_pending_tasks);
|
|
try testing.expect(!config.use_results_queue);
|
|
try testing.expectEqual(@as(usize, 64), config.num_piece_buffers);
|
|
try testing.expectEqual(@as(usize, 512 * 1024), config.piece_buffer_size);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Pool Initialization Tests
|
|
// ============================================================================
|
|
|
|
test "VerificationPool: init and deinit without buffer pool" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 0, // No buffer pool
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try testing.expect(pool.buffer_pool == null);
|
|
}
|
|
|
|
test "VerificationPool: init and deinit with buffer pool" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 4,
|
|
.piece_buffer_size = 256,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try testing.expect(pool.buffer_pool != null);
|
|
}
|
|
|
|
test "VerificationPool: start and stop" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 2,
|
|
.num_piece_buffers = 0,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
try testing.expectEqual(@as(usize, 2), pool.workers.items.len);
|
|
|
|
pool.stop();
|
|
try testing.expectEqual(@as(usize, 0), pool.workers.items.len);
|
|
}
|
|
|
|
test "VerificationPool: multiple start stop cycles" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 0,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
pool.stop();
|
|
|
|
try pool.start();
|
|
pool.stop();
|
|
}
|
|
|
|
// ============================================================================
|
|
// Task Queuing Tests
|
|
// ============================================================================
|
|
|
|
test "VerificationPool: queue verification with buffer pool" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 4,
|
|
.piece_buffer_size = 256,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
|
|
const test_data = "Test data for verification";
|
|
var expected_hash: PieceHash = undefined;
|
|
std.crypto.hash.sha2.Sha256.hash(test_data, &expected_hash, .{});
|
|
const peer_id = [_]u8{0xAA} ** 32;
|
|
|
|
try pool.queueVerification(1, 0, test_data, expected_hash, peer_id);
|
|
|
|
// Wait for result
|
|
var result: ?VerificationResult = null;
|
|
var attempts: u32 = 0;
|
|
while (result == null and attempts < 100) : (attempts += 1) {
|
|
result = pool.getResult();
|
|
if (result == null) {
|
|
std.Thread.sleep(10 * std.time.ns_per_ms);
|
|
}
|
|
}
|
|
|
|
try testing.expect(result != null);
|
|
try testing.expect(result.?.is_valid);
|
|
try testing.expectEqual(@as(u64, 1), result.?.session_id);
|
|
try testing.expectEqual(@as(u32, 0), result.?.piece_index);
|
|
}
|
|
|
|
test "VerificationPool: queue verification without buffer pool" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 0, // No buffer pool
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
|
|
const test_data = "Test data without pool";
|
|
var expected_hash: PieceHash = undefined;
|
|
std.crypto.hash.sha2.Sha256.hash(test_data, &expected_hash, .{});
|
|
const peer_id = [_]u8{0xBB} ** 32;
|
|
|
|
try pool.queueVerification(2, 1, test_data, expected_hash, peer_id);
|
|
|
|
// Wait for result
|
|
var result: ?VerificationResult = null;
|
|
var attempts: u32 = 0;
|
|
while (result == null and attempts < 100) : (attempts += 1) {
|
|
result = pool.getResult();
|
|
if (result == null) {
|
|
std.Thread.sleep(10 * std.time.ns_per_ms);
|
|
}
|
|
}
|
|
|
|
try testing.expect(result != null);
|
|
try testing.expect(result.?.is_valid);
|
|
}
|
|
|
|
test "VerificationPool: queue full error" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.max_pending_tasks = 2,
|
|
.num_piece_buffers = 0,
|
|
});
|
|
defer pool.deinit();
|
|
// Don't start workers - tasks will pile up
|
|
|
|
const test_data = "Test";
|
|
const hash = [_]u8{0} ** 32;
|
|
const peer_id = [_]u8{0xCC} ** 32;
|
|
|
|
// Fill the queue
|
|
try pool.queueVerification(1, 0, test_data, hash, peer_id);
|
|
try pool.queueVerification(2, 1, test_data, hash, peer_id);
|
|
|
|
// Third should fail with QueueFull
|
|
const result = pool.queueVerification(3, 2, test_data, hash, peer_id);
|
|
try testing.expectError(Error.QueueFull, result);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Verification Processing Tests
|
|
// ============================================================================
|
|
|
|
test "VerificationPool: invalid hash detected" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 4,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
|
|
const test_data = "Test data";
|
|
const wrong_hash = [_]u8{0xFF} ** 32; // Wrong hash
|
|
const peer_id = [_]u8{0xDD} ** 32;
|
|
|
|
try pool.queueVerification(1, 0, test_data, wrong_hash, peer_id);
|
|
|
|
// Wait for result
|
|
var result: ?VerificationResult = null;
|
|
var attempts: u32 = 0;
|
|
while (result == null and attempts < 100) : (attempts += 1) {
|
|
result = pool.getResult();
|
|
if (result == null) {
|
|
std.Thread.sleep(10 * std.time.ns_per_ms);
|
|
}
|
|
}
|
|
|
|
try testing.expect(result != null);
|
|
try testing.expect(!result.?.is_valid); // Should be invalid
|
|
}
|
|
|
|
test "VerificationPool: multiple verifications" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 2,
|
|
.num_piece_buffers = 8,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
|
|
// Queue multiple verifications
|
|
var hashes: [4]PieceHash = undefined;
|
|
const data_samples = [_][]const u8{ "Data1", "Data2", "Data3", "Data4" };
|
|
const peer_id = [_]u8{0xEE} ** 32;
|
|
|
|
for (data_samples, 0..) |data, i| {
|
|
std.crypto.hash.sha2.Sha256.hash(data, &hashes[i], .{});
|
|
try pool.queueVerification(@intCast(i), @intCast(i), data, hashes[i], peer_id);
|
|
}
|
|
|
|
// Wait for all results
|
|
var valid_count: u32 = 0;
|
|
var attempts: u32 = 0;
|
|
while (valid_count < 4 and attempts < 200) : (attempts += 1) {
|
|
if (pool.getResult()) |result| {
|
|
if (result.is_valid) {
|
|
valid_count += 1;
|
|
}
|
|
} else {
|
|
std.Thread.sleep(10 * std.time.ns_per_ms);
|
|
}
|
|
}
|
|
|
|
try testing.expectEqual(@as(u32, 4), valid_count);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Statistics Tests
|
|
// ============================================================================
|
|
|
|
test "VerificationPool: getStats reflects operations" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 4,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
|
|
// Initial stats
|
|
var stats = pool.getStats();
|
|
try testing.expectEqual(@as(u64, 0), stats.completed);
|
|
try testing.expectEqual(@as(u64, 0), stats.failed);
|
|
|
|
// Queue valid verification
|
|
const valid_data = "Valid";
|
|
var valid_hash: PieceHash = undefined;
|
|
std.crypto.hash.sha2.Sha256.hash(valid_data, &valid_hash, .{});
|
|
try pool.queueVerification(1, 0, valid_data, valid_hash, [_]u8{0} ** 32);
|
|
|
|
// Queue invalid verification
|
|
const invalid_data = "Invalid";
|
|
const wrong_hash = [_]u8{0} ** 32;
|
|
try pool.queueVerification(2, 1, invalid_data, wrong_hash, [_]u8{0} ** 32);
|
|
|
|
// Wait for completion
|
|
var attempts: u32 = 0;
|
|
while (attempts < 200) : (attempts += 1) {
|
|
stats = pool.getStats();
|
|
if (stats.completed >= 2) break;
|
|
std.Thread.sleep(10 * std.time.ns_per_ms);
|
|
}
|
|
|
|
stats = pool.getStats();
|
|
try testing.expectEqual(@as(u64, 2), stats.completed);
|
|
try testing.expectEqual(@as(u64, 1), stats.failed);
|
|
try testing.expect(stats.bytes_verified > 0);
|
|
}
|
|
|
|
test "VerificationPool: getPendingCount" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 0,
|
|
});
|
|
defer pool.deinit();
|
|
// Don't start - count pending
|
|
|
|
try testing.expectEqual(@as(usize, 0), pool.getPendingCount());
|
|
|
|
const test_data = "Test";
|
|
const hash = [_]u8{0} ** 32;
|
|
const peer_id = [_]u8{0} ** 32;
|
|
|
|
try pool.queueVerification(1, 0, test_data, hash, peer_id);
|
|
try testing.expectEqual(@as(usize, 1), pool.getPendingCount());
|
|
|
|
try pool.queueVerification(2, 1, test_data, hash, peer_id);
|
|
try testing.expectEqual(@as(usize, 2), pool.getPendingCount());
|
|
}
|
|
|
|
// ============================================================================
|
|
// Result Queue Tests
|
|
// ============================================================================
|
|
|
|
test "VerificationPool: getResult returns null when empty" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 0,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try testing.expect(pool.getResult() == null);
|
|
}
|
|
|
|
test "VerificationPool: results contain peer_id" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 4,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
|
|
const test_data = "Peer test";
|
|
var expected_hash: PieceHash = undefined;
|
|
std.crypto.hash.sha2.Sha256.hash(test_data, &expected_hash, .{});
|
|
const peer_id = [_]u8{0x42} ** 32;
|
|
|
|
try pool.queueVerification(99, 77, test_data, expected_hash, peer_id);
|
|
|
|
// Wait for result
|
|
var result: ?VerificationResult = null;
|
|
var attempts: u32 = 0;
|
|
while (result == null and attempts < 100) : (attempts += 1) {
|
|
result = pool.getResult();
|
|
if (result == null) {
|
|
std.Thread.sleep(10 * std.time.ns_per_ms);
|
|
}
|
|
}
|
|
|
|
try testing.expect(result != null);
|
|
try testing.expectEqualSlices(u8, &peer_id, &result.?.peer_id);
|
|
try testing.expectEqual(@as(u64, 99), result.?.session_id);
|
|
try testing.expectEqual(@as(u32, 77), result.?.piece_index);
|
|
}
|
|
|
|
// ============================================================================
|
|
// VerificationResult Tests
|
|
// ============================================================================
|
|
|
|
test "VerificationResult: struct fields" {
|
|
const result = VerificationResult{
|
|
.session_id = 123,
|
|
.piece_index = 456,
|
|
.is_valid = true,
|
|
.completed_at = 789,
|
|
.peer_id = [_]u8{0xAB} ** 32,
|
|
};
|
|
|
|
try testing.expectEqual(@as(u64, 123), result.session_id);
|
|
try testing.expectEqual(@as(u32, 456), result.piece_index);
|
|
try testing.expect(result.is_valid);
|
|
try testing.expectEqual(@as(i64, 789), result.completed_at);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Edge Cases
|
|
// ============================================================================
|
|
|
|
test "VerificationPool: deinit with pending tasks cleans up" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 4,
|
|
});
|
|
// Don't start workers - leave tasks pending
|
|
|
|
const test_data = "Pending task data";
|
|
const hash = [_]u8{0} ** 32;
|
|
const peer_id = [_]u8{0} ** 32;
|
|
|
|
try pool.queueVerification(1, 0, test_data, hash, peer_id);
|
|
try pool.queueVerification(2, 1, test_data, hash, peer_id);
|
|
|
|
// deinit should clean up pending tasks without leak
|
|
pool.deinit();
|
|
}
|
|
|
|
test "VerificationPool: empty data verification" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 4,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
|
|
const empty_data: []const u8 = "";
|
|
var expected_hash: PieceHash = undefined;
|
|
std.crypto.hash.sha2.Sha256.hash(empty_data, &expected_hash, .{});
|
|
const peer_id = [_]u8{0} ** 32;
|
|
|
|
try pool.queueVerification(1, 0, empty_data, expected_hash, peer_id);
|
|
|
|
// Wait for result
|
|
var result: ?VerificationResult = null;
|
|
var attempts: u32 = 0;
|
|
while (result == null and attempts < 100) : (attempts += 1) {
|
|
result = pool.getResult();
|
|
if (result == null) {
|
|
std.Thread.sleep(10 * std.time.ns_per_ms);
|
|
}
|
|
}
|
|
|
|
try testing.expect(result != null);
|
|
try testing.expect(result.?.is_valid);
|
|
}
|
|
|
|
test "VerificationPool: large data verification" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 1,
|
|
.num_piece_buffers = 2,
|
|
.piece_buffer_size = 1024 * 1024, // 1MB buffers
|
|
});
|
|
defer pool.deinit();
|
|
|
|
try pool.start();
|
|
|
|
// Create larger test data
|
|
const large_data = try allocator.alloc(u8, 64 * 1024); // 64KB
|
|
defer allocator.free(large_data);
|
|
@memset(large_data, 0xAB);
|
|
|
|
var expected_hash: PieceHash = undefined;
|
|
std.crypto.hash.sha2.Sha256.hash(large_data, &expected_hash, .{});
|
|
const peer_id = [_]u8{0} ** 32;
|
|
|
|
try pool.queueVerification(1, 0, large_data, expected_hash, peer_id);
|
|
|
|
// Wait for result
|
|
var result: ?VerificationResult = null;
|
|
var attempts: u32 = 0;
|
|
while (result == null and attempts < 100) : (attempts += 1) {
|
|
result = pool.getResult();
|
|
if (result == null) {
|
|
std.Thread.sleep(10 * std.time.ns_per_ms);
|
|
}
|
|
}
|
|
|
|
try testing.expect(result != null);
|
|
try testing.expect(result.?.is_valid);
|
|
}
|
|
|
|
test "VerificationPool: stop without start is safe" {
|
|
const allocator = testing.allocator;
|
|
|
|
var pool = try VerificationPool.init(allocator, .{
|
|
.num_workers = 2,
|
|
.num_piece_buffers = 0,
|
|
});
|
|
defer pool.deinit();
|
|
|
|
// Stop without ever starting - should be safe
|
|
pool.stop();
|
|
try testing.expectEqual(@as(usize, 0), pool.workers.items.len);
|
|
}
|