Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker scripts #10

Merged
merged 3 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,19 @@ jobs:
matrix:
tag:
- swift:5.3
#container:
# image: ${{ matrix.tag }}
# container:
# image: ${{ matrix.tag }}
steps:
- name: Checkout
uses: actions/checkout@v2
#- name: Install dependencies
# run: |
# apt-get update -qq
# apt-get install -q -y tzdata zlib1g-dev curl
#- name: Install docker-compose
# run: |
# curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o docker-compose
# chmod a+x docker-compose
# ./docker-compose --version
- name: Mosquitto container
- name: Mosquitto
run: |
docker-compose pull
docker-compose up -d
docker-compose --version
docker --version
./scripts/mosquitto.sh start
- name: Test
run: swift test --enable-test-discovery --enable-code-coverage --sanitize=thread
run: |
swift test --enable-test-discovery --enable-code-coverage --sanitize=thread
#- name: Convert coverage files
# run: |
# llvm-cov export -format="lcov" \
Expand Down
15 changes: 7 additions & 8 deletions Sources/MQTTNIO/MQTTConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import NIOWebSocket
final class MQTTConnection {
let channel: Channel
let timeout: TimeAmount?

private init(channel: Channel, timeout: TimeAmount?) {
self.channel = channel
self.timeout = timeout
}

static func create(client: MQTTClient, pingInterval: TimeAmount) -> EventLoopFuture<MQTTConnection> {
return createBootstrap(client: client, pingInterval: pingInterval)
.map { MQTTConnection(channel: $0, timeout: client.configuration.timeout)}
}

static func createBootstrap(client: MQTTClient, pingInterval: TimeAmount) -> EventLoopFuture<Channel> {
let eventLoop = client.eventLoopGroup.next()
let channelPromise = eventLoop.makePromise(of: Channel.self)
Expand All @@ -46,7 +46,7 @@ final class MQTTConnection {
let promise = eventLoop.makePromise(of: Void.self)
promise.futureResult.map { _ in channel }
.cascade(to: channelPromise)

return Self.setupChannelForWebsockets(client: client, channel: channel, upgradePromise: promise) {
return channel.pipeline.addHandlers(handlers)
}
Expand Down Expand Up @@ -133,7 +133,7 @@ final class MQTTConnection {
) -> EventLoopFuture<Void> {
// initial HTTP request handler, before upgrade
let httpHandler = WebSocketInitialRequestHandler(
host: client.host,
host: client.configuration.sniServerName ?? client.host,
urlPath: client.configuration.webSocketURLPath ?? "/mqtt",
upgradePromise: promise
)
Expand Down Expand Up @@ -179,15 +179,14 @@ final class MQTTConnection {
func sendMessageNoWait(_ message: MQTTOutboundMessage) -> EventLoopFuture<Void> {
return channel.writeAndFlush(message)
}

func close() -> EventLoopFuture<Void> {
if channel.isActive {
return channel.close()
} else {
return channel.eventLoop.makeSucceededFuture(())
}
}

var closeFuture: EventLoopFuture<Void> { channel.closeFuture }
}

12 changes: 7 additions & 5 deletions Tests/MQTTNIOTests/MQTTNIOTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import XCTest
import Foundation
import Logging
import NIO
import NIOConcurrencyHelpers
Expand All @@ -7,7 +8,8 @@ import NIOSSL
@testable import MQTTNIO

final class MQTTNIOTests: XCTestCase {

static let hostname = ProcessInfo.processInfo.environment["MOSQUITTO_SERVER"] ?? "localhost"

func connect(to client: MQTTClient) throws {
try client.connect().wait()
}
Expand Down Expand Up @@ -152,7 +154,7 @@ final class MQTTNIOTests: XCTestCase {

func createClient(identifier: String) -> MQTTClient {
MQTTClient(
host: "localhost",
host: Self.hostname,
port: 1883,
identifier: identifier,
eventLoopGroupProvider: .createNew,
Expand All @@ -162,7 +164,7 @@ final class MQTTNIOTests: XCTestCase {

func createWebSocketClient(identifier: String) -> MQTTClient {
MQTTClient(
host: "localhost",
host: Self.hostname,
port: 8080,
identifier: identifier,
eventLoopGroupProvider: .createNew,
Expand All @@ -173,7 +175,7 @@ final class MQTTNIOTests: XCTestCase {

func createSSLClient(identifier: String) throws -> MQTTClient {
return try MQTTClient(
host: "localhost",
host: Self.hostname,
port: 8883,
identifier: identifier,
eventLoopGroupProvider: .createNew,
Expand All @@ -184,7 +186,7 @@ final class MQTTNIOTests: XCTestCase {

func createWebSocketAndSSLClient(identifier: String) throws -> MQTTClient {
return try MQTTClient(
host: "localhost",
host: Self.hostname,
port: 8081,
identifier: identifier,
eventLoopGroupProvider: .createNew,
Expand Down
17 changes: 15 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
version: "3.3"

services:
# run this with docker-compose -f docker/docker-compose.yml run test
version: "3.3"

services:
test:
image: swift:5.3
working_dir: /mqtt-nio
volumes:
- .:/mqtt-nio
depends_on:
- mosquitto
environment:
- MOSQUITTO_SERVER=mosquitto
command: /bin/bash -xcl "swift test --enable-test-discovery"

mosquitto:
image: eclipse-mosquitto
volumes:
Expand All @@ -12,5 +27,3 @@ services:
- "8883:8883"
- "8080:8080"
- "8081:8081"
expose:
- "8083"
60 changes: 60 additions & 0 deletions scripts/mosquitto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

CONTAINER_ID=$(docker container ls | grep eclipse-mosquitto | awk {'print $1'})
COMMAND=$1
HOME=$(dirname $0)

usage()
{
echo "Usage: mosquitto.sh [start] [stop] [status]"
exit 2
}

start()
{
if [ -z "$CONTAINER_ID" ]; then
docker run \
-d \
-p 1883:1883 \
-p 8883:8883 \
-p 8080:8080 \
-p 8081:8081 \
-v $(pwd)/$HOME/../mosquitto/config:/mosquitto/config \
-v $(pwd)/$HOME/../mosquitto/certs:/mosquitto/certs \
eclipse-mosquitto
else
echo "Mosquitto is already running"
fi
}

stop()
{
if [ -n "$CONTAINER_ID" ]; then
echo "Stopping mosquitto"
docker container stop "$CONTAINER_ID"
docker rm "$CONTAINER_ID"
else
echo "Mosquitto is already stopped"
fi
}

status()
{
if [ -n "$CONTAINER_ID" ]; then
echo "Mosquitto is running"
else
echo "Mosquitto is not running"
fi
}

if [ "$COMMAND" == "start" ]; then
start
elif [ "$COMMAND" == "stop" ]; then
stop
elif [ "$COMMAND" == "status" ]; then
status
else
usage
exit -1
fi