add WebRTC tests

This commit is contained in:
2026-02-19 11:38:56 -03:00
parent d8111dd32a
commit e7ff1eec81
9 changed files with 2764 additions and 0 deletions

View File

@@ -0,0 +1,235 @@
//! WebRTC Cleanup and Resource Management Tests
//!
//! Verifies:
//! 1. Proper cleanup: close DataChannels, delete PeerConnections
//! 2. Edge case: destroy channel before DataChannel opens
//! 3. Edge case: destroy during ICE gathering
//! 4. Leak test: multiple connections created+closed, zero leaked allocations
//!
//! Requires: libdatachannel.dylib available
const std = @import("std");
const testing = std.testing;
const webrtc = @import("webrtc");
const rtc = webrtc.rtc;
const core = @import("core");
/// Wait for a condition with timeout (milliseconds).
fn waitFor(comptime predicate: anytype, ctx: anytype, timeout_ms: u64) bool {
var waited: u64 = 0;
const poll_ms: u64 = 10;
while (waited < timeout_ms) : (waited += poll_ms) {
if (predicate(ctx)) return true;
std.Thread.sleep(poll_ms * std.time.ns_per_ms);
}
return false;
}
test "WebRTC cleanup: destroy channel immediately after create" {
if (!rtc.load()) return error.SkipZigTest;
const allocator = testing.allocator;
rtc.rtcInitLogger(.warning, null);
var mgr = webrtc.NativeWebRTCManager.init(allocator) catch |err| switch (err) {
error.WebRTCUnavailable => return error.SkipZigTest,
};
defer mgr.deinit();
// Create and immediately destroy — should not leak
const cs = try mgr.createOffer(null);
mgr.destroyChannel(cs);
}
test "WebRTC cleanup: destroy before gathering completes" {
if (!rtc.load()) return error.SkipZigTest;
const allocator = testing.allocator;
rtc.rtcInitLogger(.warning, null);
var mgr = webrtc.NativeWebRTCManager.init(allocator) catch |err| switch (err) {
error.WebRTCUnavailable => return error.SkipZigTest,
};
defer mgr.deinit();
const cs = try mgr.createOffer(null);
// Wait just for SDP but NOT for gathering to complete
_ = waitFor(struct {
fn check(ch: *webrtc.ChannelState) bool {
return ch.getLocalSdp() != null;
}
}.check, cs, 3000);
// Destroy mid-gathering — should not crash or leak
mgr.destroyChannel(cs);
}
test "WebRTC cleanup: full connection lifecycle 10x" {
if (!rtc.load()) return error.SkipZigTest;
const allocator = testing.allocator;
rtc.rtcInitLogger(.warning, null);
var mgr = webrtc.NativeWebRTCManager.init(allocator) catch |err| switch (err) {
error.WebRTCUnavailable => return error.SkipZigTest,
};
defer mgr.deinit();
// Run 10 full connection lifecycles
var i: usize = 0;
while (i < 10) : (i += 1) {
const alice = try mgr.createOffer(null);
errdefer mgr.destroyChannel(alice);
// Wait for SDP + gathering
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.getLocalSdp() != null;
}
}.check, alice, 5000));
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.gathering_complete.load(.acquire);
}
}.check, alice, 5000));
const alice_sdp_z = try allocator.dupeZ(u8, alice.getLocalSdp().?);
defer allocator.free(alice_sdp_z);
const bob = try mgr.acceptOffer(alice_sdp_z, null);
errdefer mgr.destroyChannel(bob);
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.getLocalSdp() != null;
}
}.check, bob, 5000));
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.gathering_complete.load(.acquire);
}
}.check, bob, 5000));
// Exchange SDP
const bob_sdp_z = try allocator.dupeZ(u8, bob.getLocalSdp().?);
defer allocator.free(bob_sdp_z);
try testing.expect(rtc.rtcSetRemoteDescription(alice.pc_id, bob_sdp_z, "answer") >= 0);
// Exchange ICE candidates
{
const cands = try bob.drainLocalCandidates(allocator);
defer {
for (cands) |c| allocator.free(c);
if (cands.len > 0) allocator.free(cands);
}
for (cands) |cand| {
const z = allocator.dupeZ(u8, cand) catch continue;
defer allocator.free(z);
mgr.addRemoteCandidate(alice, z);
}
}
{
const cands = try alice.drainLocalCandidates(allocator);
defer {
for (cands) |c| allocator.free(c);
if (cands.len > 0) allocator.free(cands);
}
for (cands) |cand| {
const z = allocator.dupeZ(u8, cand) catch continue;
defer allocator.free(z);
mgr.addRemoteCandidate(bob, z);
}
}
// Wait for DataChannel open
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
return cs.dc_open.load(.acquire);
}
}.check, alice, 10000));
// Send a message
_ = try mgr.send(alice, "iteration");
// Wait for receipt
try testing.expect(waitFor(struct {
fn check(cs: *webrtc.ChannelState) bool {
cs.mutex.lock();
defer cs.mutex.unlock();
return cs.recv_queue.items.len > 0;
}
}.check, bob, 5000));
// Verify
const msg = bob.popRecvMessage().?;
defer allocator.free(msg.data);
try testing.expectEqualStrings("iteration", msg.data);
// Destroy both peers
mgr.destroyChannel(bob);
mgr.destroyChannel(alice);
}
// If we get here without test allocator detecting leaks, cleanup is correct
}
test "WebRTC cleanup: ChannelState with queued messages" {
const allocator = testing.allocator;
// Create ChannelState directly and push messages, then verify cleanup
var cs = webrtc.ChannelState.init(allocator, -1);
// Push some messages
cs.pushRecvMessage("hello");
cs.pushRecvMessage("world");
// Set SDP
cs.setLocalSdp("v=0\r\n", "offer");
// Add candidates
cs.addLocalCandidate("candidate:1 1 UDP 1 127.0.0.1 12345 typ host");
// Verify state
try testing.expect(cs.getLocalSdp() != null);
{
cs.mutex.lock();
defer cs.mutex.unlock();
try testing.expectEqual(@as(usize, 2), cs.recv_queue.items.len);
try testing.expectEqual(@as(usize, 1), cs.local_candidates.items.len);
}
// Deinit should free everything without leaks
cs.deinit();
}
test "WebRTC cleanup: PeerConnection with WebRTC transport" {
if (!rtc.load()) return error.SkipZigTest;
const allocator = testing.allocator;
const network = @import("network");
const PeerConnection = network.PeerConnection;
const Transport = core.transport.Transport;
// Create a mock-like setup: ChannelState + WebRTCChannelTransport + PeerConnection
// We can't use a real WebRTC connection here because we need the manager alive,
// but we can verify PeerConnection.initWithTransport + deinit works cleanly.
rtc.rtcInitLogger(.warning, null);
var mgr = webrtc.NativeWebRTCManager.init(allocator) catch |err| switch (err) {
error.WebRTCUnavailable => return error.SkipZigTest,
};
defer mgr.deinit();
const cs = try mgr.createOffer(null);
defer mgr.destroyChannel(cs);
var wrtc_transport = webrtc.WebRTCChannelTransport.init(&mgr, cs);
const transport = Transport.init(&wrtc_transport);
const pc = try PeerConnection.initWithTransport(allocator, transport);
try testing.expect(pc.socket == null);
// Deinit PeerConnection — should not try to close a null socket
pc.deinit();
}