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

Feature/align codestyle #513

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
85 changes: 85 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
excluded:
- Pods
- .build

line_length: 160

identifier_name:
max_length:
warning: 45
error: 60
min_length:
warning: 1

analyzer_rules:
disabled_rules:
- cyclomatic_complexity
- file_length
- force_cast
- function_body_length
- todo
- type_body_length

opt_in_rules:
- array_init
- closure_end_indentation
- closure_spacing
- collection_alignment
- contains_over_filter_count
- contains_over_filter_is_empty
- contains_over_first_not_nil
- discouraged_object_literal
- empty_count
- empty_string
- empty_xctest_method
- explicit_init
- extension_access_modifier
- fallthrough
# - file_header
# - file_name
- first_where
- flatmap_over_map_reduce
- identical_operands
- joined_default_parameter
- legacy_random
- let_var_whitespace
- last_where
- literal_expression_end_indentation
- lower_acl_than_parent
# - modifier_order
- nimble_operator
- nslocalizedstring_key
- number_separator
# - object_literal
- operator_usage_whitespace
- overridden_super_call
- override_in_extension
- pattern_matching_keywords
- private_action
- private_outlet
# - prohibited_interface_builder
- prohibited_super_call
- quick_discouraged_call
- quick_discouraged_focused_test
- quick_discouraged_pending_test
- reduce_into
- redundant_nil_coalescing
- redundant_type_annotation
- single_test_class
# - sorted_first_last
- sorted_imports
- static_operator
# - strong_iboutlet
- toggle_bool
- unavailable_function
- unneeded_parentheses_in_closure_argument
- unowned_variable_capture
- untyped_error_in_catch
- vertical_parameter_alignment_on_call
- vertical_whitespace_closing_braces
- vertical_whitespace_opening_braces
- xct_specific_matcher
- yoda_condition

number_separator:
minimum_length: 5
129 changes: 55 additions & 74 deletions CocoaMQTTTests/CocoaMQTTDeliverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,192 +6,175 @@
// Copyright © 2019 emqtt.io. All rights reserved.
//

import XCTest
@testable import CocoaMQTT
import XCTest

class CocoaMQTTDeliverTests: XCTestCase {

override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testSerialDeliver() {
let caller = Caller()
let deliver = CocoaMQTTDeliver()

let frames = [FramePublish(topic: "t/0", payload: [0x00], qos: .qos0),
FramePublish(topic: "t/1", payload: [0x01], qos: .qos1, msgid: 1),
FramePublish(topic: "t/2", payload: [0x02], qos: .qos2, msgid: 2)]

deliver.delegate = caller
for f in frames {
_ = deliver.add(f)
}
ms_sleep(100)

XCTAssertEqual(frames.count, caller.frames.count)
for i in 0 ..< frames.count {
assertEqual(frames[i], caller.frames[i])
}

}

func testAckMessage() {
let caller = Caller()
let deliver = CocoaMQTTDeliver()

let frames = [FramePublish(topic: "t/0", payload: [0x00], qos: .qos0),
FramePublish(topic: "t/1", payload: [0x01], qos: .qos1, msgid: 1),
FramePublish(topic: "t/2", payload: [0x02], qos: .qos2, msgid: 2)]

deliver.delegate = caller
for f in frames {
_ = deliver.add(f)
}

ms_sleep(100)

XCTAssertEqual(frames.count, caller.frames.count)
for i in 0 ..< frames.count {
assertEqual(frames[i], caller.frames[i])
}

var inflights = deliver.t_inflightFrames()
XCTAssertEqual(inflights.count, 2)
XCTAssertEqual(deliver.t_queuedFrames().count, 0)
for i in 0 ..< inflights.count {
assertEqual(inflights[i], frames[i+1])
assertEqual(inflights[i], frames[i + 1])
}

deliver.ack(by: FramePubAck(msgid: 1))
deliver.ack(by: FramePubRec(msgid: 2))
ms_sleep(100)

inflights = deliver.t_inflightFrames()
XCTAssertEqual(inflights.count, 1)
XCTAssertEqual(deliver.t_queuedFrames().count, 0)
assertEqual(inflights[0], FramePubRel(msgid: 2))

deliver.ack(by: FramePubComp(msgid: 2))
ms_sleep(100)

inflights = deliver.t_inflightFrames()
XCTAssertEqual(inflights.count, 0)

// Assert sent
assertEqual(caller.frames[3], FramePubRel(msgid: 2))
}

func testQueueAndInflightReDeliver() {
let caller = Caller()
let deliver = CocoaMQTTDeliver()

let frames = [FramePublish(topic: "t/0", payload: [0x00], qos: .qos0),
FramePublish(topic: "t/1", payload: [0x01], qos: .qos1, msgid: 1),
FramePublish(topic: "t/2", payload: [0x02], qos: .qos2, msgid: 2)]

deliver.retryTimeInterval = 1000
deliver.inflightWindowSize = 1
deliver.mqueueSize = 1
deliver.delegate = caller
XCTAssertEqual(true, deliver.add(frames[1]))

XCTAssertTrue(deliver.add(frames[1]))
ms_sleep(100) // Wait the message transfer to inflight-window
XCTAssertEqual(true, deliver.add(frames[2]))
XCTAssertEqual(false, deliver.add(frames[0]))
XCTAssertTrue(deliver.add(frames[2]))
XCTAssertFalse(deliver.add(frames[0]))

ms_sleep(1100) // Wait for re-delivering timeout
XCTAssertEqual(caller.frames.count, 2)
assertEqual(caller.frames[0], frames[1])
assertEqual(caller.frames[1], frames[1])

deliver.ack(by: FramePubAck(msgid: 1))
ms_sleep(100) // Waiting for the frame in the mqueue transfer to inflight window

var inflights = deliver.t_inflightFrames()
XCTAssertEqual(inflights.count, 1)
assertEqual(inflights[0], frames[2])

deliver.ack(by: FramePubRec(msgid: 2))
ms_sleep(2000) // Waiting for re-delivering timeout
deliver.ack(by: FramePubComp(msgid: 2))
ms_sleep(100)

inflights = deliver.t_inflightFrames()
XCTAssertEqual(inflights.count, 0)

let sents: [Frame] = [frames[1], frames[1], frames[2], FramePubRel(msgid: 2), FramePubRel(msgid: 2)]
XCTAssertEqual(caller.frames.count, sents.count)
for i in 0 ..< sents.count {
assertEqual(caller.frames[i], sents[i])
}
}

func testStorage() {

let clientID = "deliver-unit-testing"
let caller = Caller()
let deliver = CocoaMQTTDeliver()

let frames = [FramePublish(topic: "t/0", payload: [0x00], qos: .qos0),
FramePublish(topic: "t/1", payload: [0x01], qos: .qos1, msgid: 1),
FramePublish(topic: "t/2", payload: [0x02], qos: .qos2, msgid: 2)]

guard let storage = CocoaMQTTStorage(by: clientID) else {
XCTAssert(false, "Initial storage failed")
return
}

deliver.delegate = caller
deliver.recoverSessionBy(storage)

for f in frames {
_ = deliver.add(f)
}

var saved = storage.readAll()
XCTAssertEqual(saved.count, 2)



deliver.ack(by: FramePubAck(msgid: 1))
ms_sleep(100)
saved = storage.readAll()
XCTAssertEqual(saved.count, 1)

deliver.ack(by: FramePubRec(msgid: 2))
ms_sleep(100)
saved = storage.readAll()
XCTAssertEqual(saved.count, 1)
assertEqual(saved[0], FramePubRel(msgid: 2))



deliver.ack(by: FramePubComp(msgid: 2))
ms_sleep(100)
saved = storage.readAll()
XCTAssertEqual(saved.count, 0)

caller.reset()
_ = storage.write(frames[1])
deliver.recoverSessionBy(storage)
ms_sleep(100)
XCTAssertEqual(caller.frames.count, 1)
assertEqual(caller.frames[0], frames[1])



deliver.ack(by: FramePubAck(msgid: 1))
ms_sleep(100)
XCTAssertEqual(storage.readAll().count, 0)
}

func testTODO() {
// TODO: How to test large of messages combined qos0/qos1/qos2
}



// TODO: How to test large of messages combined qos0/qos1/qos2

// Helper for assert equality for Frame
private func assertEqual(_ f1: Frame, _ f2: Frame, _ lines: Int = #line) {
if let pub1 = f1 as? FramePublish,
Expand All @@ -200,45 +183,43 @@ class CocoaMQTTDeliverTests: XCTestCase {
XCTAssertEqual(pub1.payload(), pub2.payload())
XCTAssertEqual(pub1.msgid, pub2.msgid)
XCTAssertEqual(pub1.qos, pub2.qos)
}
else if let rel1 = f1 as? FramePubRel,
let rel2 = f2 as? FramePubRel{
} else if let rel1 = f1 as? FramePubRel,
let rel2 = f2 as? FramePubRel {
XCTAssertEqual(rel1.msgid, rel2.msgid)
} else {
XCTAssert(false, "Assert equal failed line: \(lines)")
}
}

private func ms_sleep(_ ms: Int) {
usleep(useconds_t(ms * 1000))
}
}

private class Caller: CocoaMQTTDeliverProtocol {

private let delegate_queue_key = DispatchSpecificKey<String>()
private let delegate_queue_val = "_custom_delegate_queue_"

private let delegateQueueKey = DispatchSpecificKey<String>()
private let delegateQueueVal = "_custom_delegate_queue_"

var delegateQueue: DispatchQueue

var frames = [Frame]()

init() {
delegateQueue = DispatchQueue(label: "caller.deliver.test")
delegateQueue.setSpecific(key: delegate_queue_key, value: delegate_queue_val)
delegateQueue.setSpecific(key: delegateQueueKey, value: delegateQueueVal)
}

func reset() {
frames = []
}

func deliver(_ deliver: CocoaMQTTDeliver, wantToSend frame: Frame) {
assert_in_del_queue()

frames.append(frame)
}

private func assert_in_del_queue() {
XCTAssertEqual(delegate_queue_val, DispatchQueue.getSpecific(key: delegate_queue_key))
XCTAssertEqual(delegateQueueVal, DispatchQueue.getSpecific(key: delegateQueueKey))
}
}
Loading