Skip to content

Commit

Permalink
テストを更新する
Browse files Browse the repository at this point in the history
  • Loading branch information
voluntas committed Aug 14, 2024
1 parent 60cfea1 commit 37ba8d5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 30 deletions.
8 changes: 6 additions & 2 deletions tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def __init__(
role="sendonly",
channel_id=channel_id,
metadata=metadata,
audio=False,
video=True,
audio=audio,
video=video,
video_codec_type=video_codec_type,
video_source=self._video_source,
data_channel_signaling=data_channel_signaling,
Expand Down Expand Up @@ -174,6 +174,10 @@ def connect(self):
def connected(self):
return self._connected.is_set()

@property
def switched(self):
return self._switched

def _on_track(self, track: SoraMediaTrack):
if track.kind == "audio":
pass
Expand Down
88 changes: 66 additions & 22 deletions tests/test_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(
self._channel_id: str = channel_id

self._connected: Event = Event()
self._closed: bool = False
self._switched: bool = False
self._closed: Event = Event()

self._label: str = label

Expand All @@ -42,18 +43,58 @@ def __init__(
self._is_data_channel_ready: bool = False

self._connection.on_set_offer = self._on_set_offer
self._connection.on_switched = self._on_switched
self._connection.on_notify = self._on_notify

self._connection.on_data_channel = self._on_data_channel
self._connection.on_message = self._on_message

self._connection.on_disconnect = self._on_disconnect

def connect(self):
self._connection.connect()

assert self._connected.wait(30), "Could not connect to Sora."

return self

def disconnect(self):
self._connection.disconnect()

def get_stats(self):
raw_stats = self._connection.get_stats()
return json.loads(raw_stats)

def send(self, data: bytes):
# on_data_channel() が呼ばれるまではデータチャネルの準備ができていないので待機
while not self._is_data_channel_ready and not self._closed:
time.sleep(0.01)

self._connection.send_data_channel(self._label, data)
print(f"Sent message: label={self._label}, data={data.decode()}")

@property
def connected(self):
return self._connected.is_set()

@property
def switched(self):
return self._switched

@property
def closed(self):
return self._closed.is_set()

def _on_set_offer(self, raw_offer):
offer = json.loads(raw_offer)
if offer["type"] == "offer":
self._connection_id = offer["connection_id"]

def _on_switched(self, raw_message):
message = json.loads(raw_message)
if message["type"] == "switched":
print(f"Switched DataChannel Signaling: connection_id={self._connection_id}")
self._switched = True

def _on_notify(self, raw_message):
message = json.loads(raw_message)
if (
Expand All @@ -66,8 +107,8 @@ def _on_notify(self, raw_message):

def _on_disconnect(self, error_code, message):
print(f"Disconnected Sora: error_code='{error_code}' message='{message}'")
self._closed = True
self._connected.clear()
self._closed.set()

def _on_message(self, label, data):
print(f"Received message: label={label}, data={data}")
Expand All @@ -76,24 +117,6 @@ def _on_data_channel(self, label: str):
if self._label == label:
self._is_data_channel_ready = True

def connect(self):
self._connection.connect()

assert self._connected.wait(30), "Could not connect to Sora."

return self

def send(self, data: bytes):
# on_data_channel() が呼ばれるまではデータチャネルの準備ができていないので待機
while not self._is_data_channel_ready and not self._closed:
time.sleep(0.01)

self._connection.send_data_channel(self._label, data)
print(f"Sent message: label={self._label}, data={data.decode()}")

def disconnect(self):
self._connection.disconnect()


def test_messaging_direction_recvonly(setup):
signaling_urls = setup.get("signaling_urls")
Expand All @@ -112,9 +135,30 @@ def test_messaging_direction_recvonly(setup):

time.sleep(3)

msg_sendonly.send(b"Hello, world!")
assert msg_sendonly.switched
assert msg_recvonly.switched

msg = b"Hello, world!"
msg_sendonly.send(msg)

time.sleep(3)

msg_sendonly_stats = msg_sendonly.get_stats()
msg_recvonly_stats = msg_recvonly.get_stats()

msg_recvonly.disconnect()
msg_sendonly.disconnect()

sendonly_data_channel_stats = next(
s for s in msg_sendonly_stats if s.get("type") == "data-channel" and s.get("label") == label
)
assert sendonly_data_channel_stats["state"] == "open"
assert sendonly_data_channel_stats["messagesSent"] == 1
assert sendonly_data_channel_stats["bytesSent"] == len(msg)

recvonly_data_channel_stats = next(
s for s in msg_recvonly_stats if s.get("type") == "data-channel" and s.get("label") == label
)
assert recvonly_data_channel_stats["state"] == "open"
assert recvonly_data_channel_stats["messagesReceived"] == 1
assert recvonly_data_channel_stats["bytesReceived"] == len(msg)
17 changes: 11 additions & 6 deletions tests/test_sendonly_recvonly.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def test_sendonly_recvonly_vp8(setup):
sendonly_stats = sendonly.get_stats()
recvonly_stats = recvonly.get_stats()

sendonly.disconnect()
recvonly.disconnect()

# codec が無かったら StopIteration 例外が上がる
sendonly_codec_stats = next(s for s in sendonly_stats if s.get("type") == "codec")
assert sendonly_codec_stats["mimeType"] == "video/VP8"
Expand All @@ -52,9 +55,6 @@ def test_sendonly_recvonly_vp8(setup):
assert inbound_rtp_stats["bytesReceived"] > 0
assert inbound_rtp_stats["packetsReceived"] > 0

sendonly.disconnect()
recvonly.disconnect()


def test_sendonly_recvonly_vp9(setup):
signaling_urls = setup.get("signaling_urls")
Expand All @@ -68,21 +68,29 @@ def test_sendonly_recvonly_vp9(setup):
channel_id,
metadata,
video_codec_type="VP9",
data_channel_signaling=True,
)
sendonly.connect()

recvonly = Recvonly(
signaling_urls,
channel_id,
metadata,
data_channel_signaling=True,
)
recvonly.connect()

time.sleep(5)

assert sendonly.switched
assert recvonly.switched

sendonly_stats = sendonly.get_stats()
recvonly_stats = recvonly.get_stats()

sendonly.disconnect()
recvonly.disconnect()

# codec が無かったら StopIteration 例外が上がる
sendonly_codec_stats = next(s for s in sendonly_stats if s.get("type") == "codec")
assert sendonly_codec_stats["mimeType"] == "video/VP9"
Expand All @@ -103,9 +111,6 @@ def test_sendonly_recvonly_vp9(setup):
assert inbound_rtp_stats["bytesReceived"] > 0
assert inbound_rtp_stats["packetsReceived"] > 0

sendonly.disconnect()
recvonly.disconnect()


def test_sendonly_recvonly_av1(setup):
signaling_urls = setup.get("signaling_urls")
Expand Down

0 comments on commit 37ba8d5

Please sign in to comment.