Skip to content

Commit

Permalink
Add connect-disconnect test
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Dec 24, 2024
1 parent f2951ce commit 35a7942
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
5 changes: 4 additions & 1 deletion _notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,7 @@ basic-tcp-chat.zig
https://gist.github.com/andrewrk/34c21bdc1600b0884a3ab9fa9aa485b8

Build System Tricks
https://ziggit.dev/t/build-system-tricks/3531
https://ziggit.dev/t/build-system-tricks/3531

Check connection
sudo netstat --tcp --programs --numeric|grep 11300
49 changes: 41 additions & 8 deletions src/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ pub const JobState = enum {
};

pub const Client = struct {
ready: bool = false,
mutex: Mutex = .{},
allocator: Allocator = undefined,
connection: ?Connection = null,
connection: ?*Connection = null,

/// Returns connected to beanstalkd client.
/// Arguments:
Expand All @@ -54,14 +53,48 @@ pub const Client = struct {
/// Returns errors for:
/// - failed connection
/// - already existing connection
pub fn connect(allocator: Allocator, addr: ?[]const u8, port: ?u16) !*Client {
_ = allocator;
_ = addr;
_ = port;
pub fn connect(cl: *Client, allocator: Allocator, addr: ?[]const u8, port: ?u16) !void {
cl.mutex.lock();
defer cl.mutex.unlock();

if (cl.connection != null) {
return error.AlreadyConnected;
}

cl.allocator = allocator;

var host: []const u8 = DefaultAddr;

if (addr != null) {
host = addr.?;
}

var prt: u16 = DafaultPort;

if (port != null) {
prt = port.?;
}

cl.connection = try cl.connectTcp(host, prt);

return;
}

pub fn disconnect(cl: *Client) void {
_ = cl;
cl.mutex.lock();
defer cl.mutex.unlock();

if (cl.connection == null) {
return;
}

cl.connection.?.close(cl.allocator);

cl.allocator.destroy(cl.connection.?);

cl.connection = null;

return;
}

/// Sets tube name for current produce session
Expand Down Expand Up @@ -215,7 +248,7 @@ pub const Client = struct {
const stream = try net.tcpConnectToHost(client.allocator, host, port);
errdefer stream.close();

conn = .{
conn.* = .{
.stream = stream,
.tls_client = undefined,
.protocol = Connection.Protocol.plain,
Expand Down
8 changes: 8 additions & 0 deletions src/client_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ const testing = std.testing;
const err = @import("err.zig");
const ReturnedError = err.ReturnedError;
const client = @import("client.zig");
const Client = client.Client;
const Allocator = std.mem.Allocator;

test "connect-disconnect" {
var cl: Client = .{};
try cl.connect(std.testing.allocator, null, null);
cl.disconnect();
}
2 changes: 1 addition & 1 deletion src/root_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
test {
_ = @import("name_tests.zig");
_ = @import("parse_tests.zig");
_ = @import("client.zig");
_ = @import("client_tests.zig");

@import("std").testing.refAllDecls(@This());
}

0 comments on commit 35a7942

Please sign in to comment.