add WebRTC tests
This commit is contained in:
176
tests/integration/test_webrtc_loopback.zig
Normal file
176
tests/integration/test_webrtc_loopback.zig
Normal file
@@ -0,0 +1,176 @@
|
||||
//! WebRTC Loopback Test — two native WebRTC peers exchange data.
|
||||
//!
|
||||
//! Flow:
|
||||
//! 1. Alice creates offer (SDP + ICE candidates)
|
||||
//! 2. Bob accepts Alice's offer (generates answer SDP + ICE candidates)
|
||||
//! 3. Alice receives Bob's answer and ICE candidates
|
||||
//! 4. DataChannels open on both sides
|
||||
//! 5. Alice sends "hello world", Bob receives it
|
||||
//! 6. Bob sends "hello back", Alice receives it
|
||||
//!
|
||||
//! Requires: libdatachannel.dylib available
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const webrtc = @import("webrtc");
|
||||
const rtc = webrtc.rtc;
|
||||
|
||||
/// 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 loopback: two peers exchange messages" {
|
||||
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();
|
||||
|
||||
// --- Step 1: Alice creates offer ---
|
||||
const alice = try mgr.createOffer(null);
|
||||
defer mgr.destroyChannel(alice);
|
||||
|
||||
// Wait for Alice's SDP to be generated
|
||||
const got_alice_sdp = waitFor(struct {
|
||||
fn check(cs: *webrtc.ChannelState) bool {
|
||||
return cs.getLocalSdp() != null;
|
||||
}
|
||||
}.check, alice, 5000);
|
||||
try testing.expect(got_alice_sdp);
|
||||
|
||||
// Wait for Alice's ICE gathering to complete
|
||||
const alice_gathered = waitFor(struct {
|
||||
fn check(cs: *webrtc.ChannelState) bool {
|
||||
return cs.gathering_complete.load(.acquire);
|
||||
}
|
||||
}.check, alice, 5000);
|
||||
try testing.expect(alice_gathered);
|
||||
|
||||
// Get Alice's offer SDP (need null-terminated copy)
|
||||
const alice_sdp_slice = alice.getLocalSdp().?;
|
||||
const alice_sdp_z = try allocator.dupeZ(u8, alice_sdp_slice);
|
||||
defer allocator.free(alice_sdp_z);
|
||||
|
||||
// --- Step 2: Bob accepts Alice's offer ---
|
||||
const bob = try mgr.acceptOffer(alice_sdp_z, null);
|
||||
defer mgr.destroyChannel(bob);
|
||||
|
||||
// Wait for Bob's answer SDP
|
||||
const got_bob_sdp = waitFor(struct {
|
||||
fn check(cs: *webrtc.ChannelState) bool {
|
||||
return cs.getLocalSdp() != null;
|
||||
}
|
||||
}.check, bob, 5000);
|
||||
try testing.expect(got_bob_sdp);
|
||||
|
||||
// Wait for Bob's ICE gathering
|
||||
const bob_gathered = waitFor(struct {
|
||||
fn check(cs: *webrtc.ChannelState) bool {
|
||||
return cs.gathering_complete.load(.acquire);
|
||||
}
|
||||
}.check, bob, 5000);
|
||||
try testing.expect(bob_gathered);
|
||||
|
||||
// --- Step 3: Exchange Bob's answer back to Alice ---
|
||||
const bob_sdp_slice = bob.getLocalSdp().?;
|
||||
const bob_sdp_z = try allocator.dupeZ(u8, bob_sdp_slice);
|
||||
defer allocator.free(bob_sdp_z);
|
||||
|
||||
const result = rtc.rtcSetRemoteDescription(alice.pc_id, bob_sdp_z, "answer");
|
||||
try testing.expect(result >= 0);
|
||||
|
||||
// Exchange ICE candidates: Bob → Alice
|
||||
{
|
||||
const bob_candidates = try bob.drainLocalCandidates(allocator);
|
||||
defer {
|
||||
for (bob_candidates) |c| allocator.free(c);
|
||||
if (bob_candidates.len > 0) allocator.free(bob_candidates);
|
||||
}
|
||||
for (bob_candidates) |cand| {
|
||||
const cand_z = allocator.dupeZ(u8, cand) catch continue;
|
||||
defer allocator.free(cand_z);
|
||||
mgr.addRemoteCandidate(alice, cand_z);
|
||||
}
|
||||
}
|
||||
|
||||
// Exchange ICE candidates: Alice → Bob
|
||||
{
|
||||
const alice_candidates = try alice.drainLocalCandidates(allocator);
|
||||
defer {
|
||||
for (alice_candidates) |c| allocator.free(c);
|
||||
if (alice_candidates.len > 0) allocator.free(alice_candidates);
|
||||
}
|
||||
for (alice_candidates) |cand| {
|
||||
const cand_z = allocator.dupeZ(u8, cand) catch continue;
|
||||
defer allocator.free(cand_z);
|
||||
mgr.addRemoteCandidate(bob, cand_z);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Step 4: Wait for DataChannels to open ---
|
||||
const alice_open = waitFor(struct {
|
||||
fn check(cs: *webrtc.ChannelState) bool {
|
||||
return cs.dc_open.load(.acquire);
|
||||
}
|
||||
}.check, alice, 10000);
|
||||
try testing.expect(alice_open);
|
||||
|
||||
const bob_open = waitFor(struct {
|
||||
fn check(cs: *webrtc.ChannelState) bool {
|
||||
return cs.dc_open.load(.acquire);
|
||||
}
|
||||
}.check, bob, 10000);
|
||||
try testing.expect(bob_open);
|
||||
|
||||
// --- Step 5: Alice sends to Bob ---
|
||||
const msg = "hello world";
|
||||
const sent = try mgr.send(alice, msg);
|
||||
try testing.expectEqual(msg.len, sent);
|
||||
|
||||
// Wait for Bob to receive
|
||||
const bob_received = 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);
|
||||
try testing.expect(bob_received);
|
||||
|
||||
// Read Bob's received message
|
||||
const recv_msg = bob.popRecvMessage().?;
|
||||
defer allocator.free(recv_msg.data);
|
||||
try testing.expectEqualStrings(msg, recv_msg.data);
|
||||
|
||||
// --- Step 6: Bob sends back to Alice ---
|
||||
const reply = "hello back";
|
||||
const bob_dc = bob.dc_id.load(.acquire);
|
||||
try testing.expect(bob_dc >= 0);
|
||||
const reply_result = rtc.rtcSendMessage(bob_dc, reply.ptr, @intCast(reply.len));
|
||||
try testing.expect(reply_result >= 0);
|
||||
|
||||
// Wait for Alice to receive
|
||||
const alice_received = waitFor(struct {
|
||||
fn check(cs: *webrtc.ChannelState) bool {
|
||||
cs.mutex.lock();
|
||||
defer cs.mutex.unlock();
|
||||
return cs.recv_queue.items.len > 0;
|
||||
}
|
||||
}.check, alice, 5000);
|
||||
try testing.expect(alice_received);
|
||||
|
||||
const alice_recv = alice.popRecvMessage().?;
|
||||
defer allocator.free(alice_recv.data);
|
||||
try testing.expectEqualStrings(reply, alice_recv.data);
|
||||
}
|
||||
Reference in New Issue
Block a user