Skip to content

Commit

Permalink
Added unit tests for error handling (#16)
Browse files Browse the repository at this point in the history
### TL;DR
Added network output handler with test coverage for initialization and deinitialization

### What changed?
- Added network module export in nexlog.zig
- Implemented test case for NetworkHandler initialization in network.zig
- Created new network_tests.zig file with comprehensive handler testing
- Added validation for endpoint configuration and buffer initialization

### How to test?
1. Run the test suite using `zig test`
2. Verify NetworkHandler initialization with example.com:8080
3. Check buffer size and endpoint configuration assertions
4. Confirm proper deinitialization of resources

### Why make this change?
To ensure the network output handler is properly tested and validated, providing confidence in its initialization, configuration handling, and resource management. This is crucial for maintaining reliability when sending logs over network connections.
  • Loading branch information
chrischtel authored Jan 29, 2025
2 parents a9d3f1c + e458190 commit 91bba34
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/nexlog.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub const output = struct {
pub const console = @import("output/console.zig");
pub const file = @import("output/file.zig");
pub const handler = @import("output/handlers.zig");
pub const network = @import("output/network.zig");
};

// Re-export main types and functions
Expand Down
20 changes: 20 additions & 0 deletions src/output/network.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const types = @import("../core/types.zig");
const errors = @import("../core/errors.zig");
const buffer = @import("../utils/buffer.zig");
const expect = std.testing.expect;

pub const NetworkEndpoint = struct {
host: []const u8,
Expand Down Expand Up @@ -167,3 +168,22 @@ pub const NetworkHandler = struct {
return stream;
}
};

test "NetworkHandler initialization and deinitialization" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();

const config = NetworkConfig{
.endpoint = .{
.host = "example.com",
.port = 8080,
},
};

var handler = try NetworkHandler.init(allocator, config);
defer handler.deinit();

try expect(handler.config.endpoint.host.len == config.endpoint.host.len);
try expect(handler.config.endpoint.port == config.endpoint.port);
try expect(handler.circular_buffer.buffer.len == config.buffer_size);
}
26 changes: 26 additions & 0 deletions tests/network_tests.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const std = @import("std");
const expect = std.testing.expect;
const NetworkHandler = @import("nexlog").output.network.NetworkHandler;
const NetworkConfig = @import("nexlog").output.network.NetworkConfig;

test "NetworkHandler initialization and deinitialization" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();

const config = NetworkConfig{
.endpoint = .{
.host = "example.com",
.port = 8080,
.secure = true,
},
};

var handler = try NetworkHandler.init(allocator, config);
defer handler.deinit();

try expect(handler.config.endpoint.host.len == config.endpoint.host.len);
try expect(handler.config.endpoint.port == config.endpoint.port);
try expect(handler.circular_buffer.buffer.len == config.buffer_size);

std.debug.print("NetworkHandler initialized\n", .{});
}

0 comments on commit 91bba34

Please sign in to comment.