Skip to content

Commit

Permalink
Merge pull request #348 from dvonthenen/restructure-common-test-cases
Browse files Browse the repository at this point in the history
Examples to Demonstrate KeepAlive and Failure for Not Using It
  • Loading branch information
davidvonthenen authored Mar 27, 2024
2 parents 95e2b81 + 2224bb8 commit c601e75
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
33 changes: 33 additions & 0 deletions tests/edge_cases/keepalive/async/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

import asyncio
import time
import logging, verboselogs

from deepgram import DeepgramClient, DeepgramClientOptions, LiveOptions


async def main():
# for debugging
config: DeepgramClientOptions = DeepgramClientOptions(
verbose=logging.DEBUG, options={"keepalive": "true"}
)
deepgram: DeepgramClient = DeepgramClient("", config)

deepgram_connection = deepgram.listen.asynclive.v("1")

await deepgram_connection.start(LiveOptions())

# Wait for a while to simulate a long-running connection
await asyncio.sleep(600)

print("deadlock!")
try:
await deepgram_connection.finish()
finally:
print("no deadlock...")


asyncio.run(main())
35 changes: 35 additions & 0 deletions tests/edge_cases/keepalive/sync/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

import time
import logging, verboselogs

from deepgram import DeepgramClient, DeepgramClientOptions, LiveOptions


def main():
# for debugging
config: DeepgramClientOptions = DeepgramClientOptions(
verbose=logging.DEBUG, options={"keepalive": "true"}
)
deepgram: DeepgramClient = DeepgramClient("", config)
# OR
# deepgram: DeepgramClient = DeepgramClient()

deepgram_connection = deepgram.listen.live.v("1")

deepgram_connection.start(LiveOptions())

# press any key to exit
input("\n\nPress Enter to exit...\n\n")

print("deadlock!")
try:
deepgram_connection.finish()
finally:
print("no deadlock...")


if __name__ == "__main__":
main()
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@


async def main():
# for debugging
config: DeepgramClientOptions = DeepgramClientOptions(verbose=logging.DEBUG)
deepgram: DeepgramClient = DeepgramClient("", config)
# OR
# deepgram: DeepgramClient = DeepgramClient()

deepgram_connection = deepgram.listen.asynclive.v("1")

await deepgram_connection.start(LiveOptions())

time.sleep(
30
) # Deepgram will close the connection after 10-15s of silence, followed with another 5 seconds for a ping
# Deepgram will close the connection after 10-15s of silence, followed with another 5 seconds for a ping
await asyncio.sleep(30)

print("deadlock!")
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@


def main():
config: DeepgramClientOptions = DeepgramClientOptions(verbose=logging.DEBUG)
# for debugging
config: DeepgramClientOptions = DeepgramClientOptions(verbose=logging.SPAM)
deepgram: DeepgramClient = DeepgramClient("", config)
# OR
# deepgram: DeepgramClient = DeepgramClient()

deepgram_connection = deepgram.listen.live.v("1")

deepgram_connection.start(LiveOptions())

time.sleep(
30
) # Deepgram will close the connection after 10-15s of silence, followed with another 5 seconds for a ping
# Deepgram will close the connection after 10-15s of silence, followed with another 5 seconds for a ping
time.sleep(30)

print("deadlock!")
try:
Expand Down

0 comments on commit c601e75

Please sign in to comment.