-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
on_message (any publish) + MQTT tests (#94)
* Updated CRT libs * Added boto3 as a depedency for testing * Fixed self-referencing in MQTT connection * Added support for null sub callbacks
- Loading branch information
Justin Boswell
authored
Nov 19, 2019
1 parent
a34bb20
commit c10c6d2
Showing
11 changed files
with
208 additions
and
33 deletions.
There are no files selected for viewing
Submodule aws-c-common
updated
23 files
+1 −0 | codebuild/builder.py | |
+50 −0 | include/aws/common/allocator.h | |
+0 −7 | include/aws/common/assert.h | |
+12 −0 | include/aws/common/byte_buf.h | |
+1 −0 | include/aws/common/error.h | |
+1 −0 | include/aws/common/logging.h | |
+2 −2 | include/aws/common/macros.h | |
+44 −0 | include/aws/common/system_info.h | |
+36 −74 | include/aws/testing/aws_test_harness.h | |
+2 −2 | source/assert.c | |
+38 −0 | source/byte_buf.c | |
+4 −0 | source/common.c | |
+519 −0 | source/memtrace.c | |
+118 −5 | source/posix/system_info.c | |
+115 −28 | source/windows/system_info.c | |
+9 −0 | tests/CMakeLists.txt | |
+0 −2 | tests/assert_test.c | |
+82 −0 | tests/byte_cursor_find_test.c | |
+3 −1 | tests/logging/logging_test_utilities.c | |
+17 −5 | tests/logging/test_logger.c | |
+7 −3 | tests/logging/test_logger.h | |
+253 −0 | tests/memtrace_test.c | |
+33 −39 | tests/system_info_tests.c |
Submodule aws-c-io
updated
13 files
+1 −1 | .clang-tidy | |
+52 −39 | source/darwin/darwin_pki_utils.c | |
+3 −4 | source/darwin/secure_transport_tls_channel_handler.c | |
+7 −3 | source/host_resolver.c | |
+14 −4 | source/linux/epoll_event_loop.c | |
+1 −1 | source/posix/host_resolver.c | |
+5 −6 | source/s2n/s2n_tls_channel_handler.c | |
+1 −1 | source/windows/host_resolver.c | |
+4 −9 | source/windows/secure_channel_tls_handler.c | |
+6 −4 | tests/CMakeLists.txt | |
+1 −1 | tests/channel_test.c | |
+31 −1 | tests/event_loop_test.c | |
+18 −5 | tests/tls_handler_test.c |
Submodule aws-c-mqtt
updated
12 files
+0 −4 | .gitignore | |
+1 −1 | CMakeLists.txt | |
+1 −1 | builder.json | |
+35 −1 | include/aws/mqtt/client.h | |
+8 −0 | include/aws/mqtt/private/client_impl.h | |
+5 −4 | include/aws/mqtt/private/topic_tree.h | |
+282 −24 | source/client.c | |
+8 −0 | source/packets.c | |
+14 −5 | source/topic_tree.c | |
+5 −3 | tests/aws_iot_client_test.c | |
+6 −4 | tests/paho_client_test.c | |
+10 −4 | tests/topic_tree_test.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule s2n
updated
from 4675f7 to f58bc0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# A copy of the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0 | ||
# | ||
# or in the "license" file accompanying this file. This file is distributed | ||
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
# express or implied. See the License for the specific language governing | ||
# permissions and limitations under the License. | ||
|
||
from __future__ import absolute_import | ||
from awscrt.io import ClientBootstrap, ClientTlsContext, DefaultHostResolver, EventLoopGroup, TlsConnectionOptions, TlsContextOptions, LogLevel, init_logging | ||
from awscrt.mqtt import Client, Connection, QoS | ||
from test import NativeResourceTest | ||
from concurrent.futures import Future | ||
import os | ||
import unittest | ||
import boto3 | ||
import time | ||
import warnings | ||
|
||
|
||
class MqttClientTest(NativeResourceTest): | ||
def test_lifetime(self): | ||
client = Client(ClientBootstrap(EventLoopGroup())) | ||
|
||
|
||
class Config: | ||
cache = None | ||
|
||
def __init__(self, endpoint, cert, key): | ||
try: | ||
self.cert = cert | ||
self.key = key | ||
self.endpoint = endpoint | ||
self.valid = True | ||
except BaseException: | ||
self.valid = False | ||
|
||
@staticmethod | ||
def get(): | ||
if Config.cache: | ||
return Config.cache | ||
|
||
# boto3 caches the HTTPS connection for the API calls, which appears to the unit test | ||
# framework as a leak, so ignore it, that's not what we're testing here | ||
warnings.simplefilter('ignore', ResourceWarning) | ||
|
||
secrets = boto3.client('secretsmanager') | ||
response = secrets.get_secret_value(SecretId='unit-test/endpoint') | ||
endpoint = response['SecretString'] | ||
response = secrets.get_secret_value(SecretId='unit-test/certificate') | ||
cert = bytes(response['SecretString'], 'utf8') | ||
response = secrets.get_secret_value(SecretId='unit-test/privatekey') | ||
key = bytes(response['SecretString'], 'utf8') | ||
Config.cache = Config(endpoint, cert, key) | ||
return Config.cache | ||
|
||
|
||
class MqttConnectionTest(NativeResourceTest): | ||
TEST_TOPIC = '/test/me/senpai' | ||
TEST_MSG = 'NOTICE ME!' | ||
|
||
def _test_connection(self): | ||
try: | ||
config = Config.get() | ||
except Exception as ex: | ||
return self.skipTest("No credentials") | ||
|
||
try: | ||
tls_opts = TlsContextOptions.create_client_with_mtls(config.cert, config.key) | ||
tls = ClientTlsContext(tls_opts) | ||
client = Client(ClientBootstrap(EventLoopGroup()), tls) | ||
connection = Connection(client) | ||
connection.connect('aws-crt-python-unit-test-'.format(time.gmtime()), config.endpoint, 8883).result() | ||
return connection | ||
except Exception as ex: | ||
self.assertFalse(ex) | ||
|
||
def test_connect_disconnect(self): | ||
connection = self._test_connection() | ||
connection.disconnect().result() | ||
|
||
def test_pub_sub(self): | ||
connection = self._test_connection() | ||
disconnected = Future() | ||
|
||
def on_disconnect(result): | ||
disconnected.set_result(True) | ||
|
||
def on_message(topic, payload): | ||
self.assertEqual(self.TEST_TOPIC, topic) | ||
self.assertEqual(self.TEST_MSG, str(payload, 'utf8')) | ||
connection.unsubscribe(self.TEST_TOPIC) | ||
connection.disconnect().add_done_callback(on_disconnect) | ||
|
||
def do_publish(result): | ||
connection.publish(self.TEST_TOPIC, bytes(self.TEST_MSG, 'utf8'), QoS.AT_LEAST_ONCE) | ||
|
||
subscribed, packet_id = connection.subscribe(self.TEST_TOPIC, QoS.AT_LEAST_ONCE, on_message) | ||
subscribed.add_done_callback(do_publish) | ||
|
||
disconnected.result() | ||
|
||
def test_on_message(self): | ||
connection = self._test_connection() | ||
disconnected = Future() | ||
|
||
def on_disconnect(result): | ||
disconnected.set_result(True) | ||
|
||
def on_message(topic, payload): | ||
self.assertEqual(self.TEST_TOPIC, topic) | ||
self.assertEqual(self.TEST_MSG, str(payload, 'utf8')) | ||
connection.unsubscribe(self.TEST_TOPIC) | ||
connection.disconnect().add_done_callback(on_disconnect) | ||
|
||
def do_publish(result): | ||
connection.publish(self.TEST_TOPIC, bytes(self.TEST_MSG, 'utf8'), QoS.AT_LEAST_ONCE) | ||
|
||
connection.on_message(on_message) | ||
subscribed, packet_id = connection.subscribe(self.TEST_TOPIC, QoS.AT_LEAST_ONCE) | ||
subscribed.add_done_callback(do_publish) | ||
|
||
disconnected.result() | ||
|
||
|
||
if __name__ == 'main': | ||
unittest.main() |