Skip to content

Commit

Permalink
Add Starscream as a pod
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhijeetMallick committed Apr 8, 2024
1 parent fd85313 commit 872cb97
Show file tree
Hide file tree
Showing 32 changed files with 271 additions and 3,120 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions Clickstream.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Pod::Spec.new do |s|
s.dependency "SwiftProtobuf", "~> 1.10"
s.dependency "ReachabilitySwift", '>= 5.0.0'
s.dependency "GRDB.swift", "~> 6.7"
s.dependency "Starscream", "~> 4.0"

s.default_subspec = 'Core'

Expand Down
250 changes: 37 additions & 213 deletions Clickstream.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ class RetryMechanismTests: XCTestCase {
let deviceStatus = DefaultDeviceStatus(performOnQueue: mockQueue)
let networkService = DefaultNetworkService<SocketHandlerMockSuccess>(with: config, performOnQueue: .main)
let persistence = DefaultDatabaseDAO<EventRequest>(database: database, performOnQueue: dbQueueMock)
let keepAliveService = DefaultKeepAliveService(with: mockQueue, duration: 2, reachability: NetworkReachabilityMock(isReachable: false))
let keepAliveService = DefaultKeepAliveServiceWithSafeTimer(with: mockQueue, duration: 2, reachability: NetworkReachabilityMock(isReachable: false))

//when
let sut = DefaultRetryMechanism(networkService: networkService, reachability: NetworkReachabilityMock(isReachable: true), deviceStatus: deviceStatus, appStateNotifier: AppStateNotifierMock(state: .didBecomeActive), performOnQueue: mockQueue, persistence: persistence, keepAliveService: keepAliveService)

persistence.deleteAll()

let mockEventRequest: EventRequest = EventRequest(guid: UUID().uuidString, data: Data())

sut.trackBatch(with: mockEventRequest)

DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 3) {
if (mockEventRequest.eventType != .instant), let fetchedRequest = persistence.fetchAll()?.first {
if fetchedRequest.retriesMade > 0 {
Expand All @@ -56,7 +56,7 @@ class RetryMechanismTests: XCTestCase {
}

//then
wait(for: [expectation], timeout: 5.0)
wait(for: [expectation], timeout: 10.0)
}

func test_whenTheMaxRetriesAreReached_thenTheBatchMustGetRemovedFromTheCache() {
Expand Down
18 changes: 14 additions & 4 deletions ClickstreamTests/NetworkManagerTests/Mocks/SocketHandlerMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ enum SocketConnectionState {
}

final class SocketHandlerMockSuccess: SocketHandler {

static var state: SocketConnectionState = .successWithData

private let connectionCallback: ConnectionStatus?
init(performOnQueue: SerialQueue) {

}

func setup(request: URLRequest, keepTrying: Bool, connectionCallback: ConnectionStatus?) {
self.connectionCallback = connectionCallback
SerialQueue.main.asyncAfter(deadline: .now() + 0.5) {
connectionCallback?(.success(.connected))//change this. -AV
}
}

private var connectionCallback: ConnectionStatus?

func sendPing(_ data: Data) { }

Expand Down Expand Up @@ -50,7 +60,7 @@ final class SocketHandlerMockSuccess: SocketHandler {
connectionCallback?(.success(.disconnected))
}

var isConnected: Bool {
true
var isConnected: Atomic<Bool> {
return Atomic(true)
}
}
24 changes: 21 additions & 3 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -167,6 +167,7 @@
9AEA264824924B340018931D /* Sources */,
9AEA264924924B340018931D /* Frameworks */,
9AEA264A24924B340018931D /* Resources */,
3F6D58C8F488CB53E6EFC4AD /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -225,6 +226,23 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
3F6D58C8F488CB53E6EFC4AD /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Example/Pods-Example-resources-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Copy Pods Resources";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Example/Pods-Example-resources-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Example/Pods-Example-resources.sh\"\n";
showEnvVarsInLog = 0;
};
685A269624A9E10A001B58D0 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -429,7 +447,7 @@
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -461,7 +479,7 @@
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
3 changes: 2 additions & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '11.0'
platform :ios, '12.0'
use_modular_headers!

project 'Clickstream.xcodeproj'
Expand All @@ -12,6 +12,7 @@ def clickstream_pods
pod 'SwiftProtobuf', '~> 1.10'
pod 'ReachabilitySwift', '>= 5.0.0'
pod 'GRDB.swift', '~> 6.7'
pod 'Starscream', '~> 4.0.6'
end

target 'Clickstream' do
Expand Down
6 changes: 5 additions & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ PODS:
- GRDB.swift/standard (= 6.24.1)
- GRDB.swift/standard (6.24.1)
- ReachabilitySwift (5.0.0)
- Starscream (4.0.8)
- SwiftProtobuf (1.25.2)

DEPENDENCIES:
- GRDB.swift (~> 6.7)
- ReachabilitySwift (>= 5.0.0)
- Starscream (~> 4.0.6)
- SwiftProtobuf (~> 1.10)

SPEC REPOS:
https://github.com/CocoaPods/Specs.git:
- GRDB.swift
- ReachabilitySwift
- Starscream
- SwiftProtobuf

SPEC CHECKSUMS:
GRDB.swift: 136dcb5d8dddca50aae3ba7d77475f79e7232cd8
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825
Starscream: 19b5533ddb925208db698f0ac508a100b884a1b9
SwiftProtobuf: 407a385e97fd206c4fbe880cc84123989167e0d1

PODFILE CHECKSUM: f24f9da678cf7c77fd3275d27f766a88682ecadf
PODFILE CHECKSUM: 12f0fcd6592ee203f8b3339e7ec80ee9771693c0

COCOAPODS: 1.15.2
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ protocol ConnectableInputs {

/// Initializer
/// - Parameters:
/// - request: URLRequest which the connectable must connect to.
/// - keepTrying: A control flag which tells the connectable to keep trying till the connection is not established.
/// - performOnQueue: A queue instance on which the tasks are performed.
/// - connectionCallback: A callback to update about the connection status.
init(request: URLRequest, keepTrying: Bool, performOnQueue: SerialQueue, connectionCallback: ConnectionStatus?)
init(performOnQueue: SerialQueue)


/// Writes data to the stream.
Expand All @@ -58,13 +55,20 @@ protocol ConnectableInputs {
/// Disconnects the connection.
func disconnect()

// func connect(with request: URLRequest, _ completion: @escaping ((Result<[String: String]?, ConnectionError>) -> Void))
/// Sets up a connectable
/// - Parameters:
/// - request: URLRequest which the connectable must connect to.
/// - keepTrying: A control flag which tells the connectable to keep trying till the connection is not established.
/// - connectionCallback: A callback to update about the connection status.
func setup(request: URLRequest,
keepTrying: Bool,
connectionCallback: ConnectionStatus?)
}

protocol ConnectableOutputs {

/// Returns the connection state.
var isConnected: Bool { get }
var isConnected: Atomic<Bool> { get }
}

protocol Connectable: ConnectableInputs, ConnectableOutputs { }
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ protocol NetworkServiceInputs {
/// - Parameters:
/// - connectionStatusListener: A callback to listen to the change in the status.
/// - keepTrying: allow connectable to try reconnection exponentially
@discardableResult func initiateConnection(connectionStatusListener: ConnectionStatus?,
keepTrying: Bool) -> Connectable?
func initiateConnection(connectionStatusListener: ConnectionStatus?,
keepTrying: Bool)

/// Writes data to the given connectable and fires a completion event after the write is completed.
/// - Parameters:
Expand Down Expand Up @@ -77,15 +77,14 @@ final class DefaultNetworkService<C: Connectable>: NetworkService {
extension DefaultNetworkService {

func initiateConnection(connectionStatusListener: ConnectionStatus?,
keepTrying: Bool = false) -> Connectable? {
guard _connectable == nil else { return self._connectable }
keepTrying: Bool = false) {
guard _connectable == nil else { return }
self.connectionCallback = connectionStatusListener
let request = networkConfig.request
_connectable = C(request: request,
keepTrying: false,
performOnQueue: performQueue,
connectionCallback: self.connectionCallback)
return _connectable
_connectable = C(performOnQueue: performQueue)
connectable?.setup(request: request,
keepTrying: false,
connectionCallback: self.connectionCallback)
}

func write<T>(_ data: Data, completion: @escaping (Result<T, ConnectableError>) -> Void) where T : Message {
Expand Down Expand Up @@ -129,6 +128,6 @@ extension DefaultNetworkService {
extension DefaultNetworkService {

var isConnected: Bool {
_connectable?.isConnected ?? false
_connectable?.isConnected.value ?? false
}
}
Loading

0 comments on commit 872cb97

Please sign in to comment.