@@ -142,10 +142,11 @@ final class Analytics_Tests: XCTestCase {
142
142
let expectation = XCTestExpectation ( description: " MyDestination Expectation " )
143
143
let myDestination = MyDestination ( disabled: true ) {
144
144
expectation. fulfill ( )
145
+ print ( " called " )
145
146
return true
146
147
}
147
148
148
- let configuration = Configuration ( writeKey: " test " )
149
+ let configuration = Configuration ( writeKey: " testDestNotEnabled " )
149
150
let analytics = Analytics ( configuration: configuration)
150
151
151
152
analytics. add ( plugin: myDestination)
@@ -754,25 +755,36 @@ final class Analytics_Tests: XCTestCase {
754
755
. flushAt ( 9999 )
755
756
. operatingMode ( . asynchronous) )
756
757
758
+ // set the httpclient to use our blocker session
759
+ let segment = analytics. find ( pluginType: SegmentDestination . self)
760
+ let configuration = URLSessionConfiguration . ephemeral
761
+ configuration. allowsCellularAccess = true
762
+ configuration. timeoutIntervalForResource = 30
763
+ configuration. timeoutIntervalForRequest = 60
764
+ configuration. httpMaximumConnectionsPerHost = 2
765
+ configuration. protocolClasses = [ BlockNetworkCalls . self]
766
+ configuration. httpAdditionalHeaders = [ " Content-Type " : " application/json; charset=utf-8 " ,
767
+ " Authorization " : " Basic test " ,
768
+ " User-Agent " : " analytics-ios/ \( Analytics . version ( ) ) " ]
769
+ let blockSession = URLSession ( configuration: configuration, delegate: nil , delegateQueue: nil )
770
+ segment? . httpClient? . session = blockSession
771
+
757
772
waitUntilStarted ( analytics: analytics)
758
773
759
774
analytics. storage. hardReset ( doYouKnowHowToUseThis: true )
760
775
761
- @ Atomic var completionCalled = false
776
+ let expectation = XCTestExpectation ( )
762
777
763
778
// put an event in the pipe ...
764
779
analytics. track ( name: " completion test1 " )
765
780
// flush it, that'll get us an upload going
766
781
analytics. flush {
767
782
// verify completion is called.
768
- completionCalled = true
783
+ expectation . fulfill ( )
769
784
}
770
785
771
- while !completionCalled {
772
- RunLoop . main. run ( until: Date . distantPast)
773
- }
786
+ wait ( for: [ expectation] , timeout: 5 )
774
787
775
- XCTAssertTrue ( completionCalled)
776
788
XCTAssertNil ( analytics. pendingUploads)
777
789
}
778
790
@@ -783,22 +795,35 @@ final class Analytics_Tests: XCTestCase {
783
795
. flushAt ( 9999 )
784
796
. operatingMode ( . synchronous) )
785
797
798
+ // set the httpclient to use our blocker session
799
+ let segment = analytics. find ( pluginType: SegmentDestination . self)
800
+ let configuration = URLSessionConfiguration . ephemeral
801
+ configuration. allowsCellularAccess = true
802
+ configuration. timeoutIntervalForResource = 30
803
+ configuration. timeoutIntervalForRequest = 60
804
+ configuration. httpMaximumConnectionsPerHost = 2
805
+ configuration. protocolClasses = [ BlockNetworkCalls . self]
806
+ configuration. httpAdditionalHeaders = [ " Content-Type " : " application/json; charset=utf-8 " ,
807
+ " Authorization " : " Basic test " ,
808
+ " User-Agent " : " analytics-ios/ \( Analytics . version ( ) ) " ]
809
+ let blockSession = URLSession ( configuration: configuration, delegate: nil , delegateQueue: nil )
810
+ segment? . httpClient? . session = blockSession
811
+
786
812
waitUntilStarted ( analytics: analytics)
787
813
788
814
analytics. storage. hardReset ( doYouKnowHowToUseThis: true )
789
815
790
- @Atomic var completionCalled = false
791
-
816
+ let expectation = XCTestExpectation ( )
792
817
// put an event in the pipe ...
793
818
analytics. track ( name: " completion test1 " )
794
819
// flush it, that'll get us an upload going
795
820
analytics. flush {
796
821
// verify completion is called.
797
- completionCalled = true
822
+ expectation . fulfill ( )
798
823
}
799
824
800
- // completion shouldn't be called before flush returned.
801
- XCTAssertTrue ( completionCalled )
825
+ wait ( for : [ expectation ] , timeout : 1 )
826
+
802
827
XCTAssertNil ( analytics. pendingUploads)
803
828
804
829
// put another event in the pipe.
@@ -921,4 +946,74 @@ final class Analytics_Tests: XCTestCase {
921
946
XCTAssertFalse ( FileManager . default. fileExists ( atPath: fileURL. path) )
922
947
}
923
948
#endif
949
+
950
+ func testAnonIDGenerator( ) throws {
951
+ class MyAnonIdGenerator : AnonymousIdGenerator {
952
+ var currentId : String = " blah- "
953
+ func newAnonymousId( ) -> String {
954
+ currentId = currentId + " 1 "
955
+ return currentId
956
+ }
957
+ }
958
+
959
+ // need to clear settings for this one.
960
+ UserDefaults . standard. removePersistentDomain ( forName: " com.segment.storage.anonIdGenerator " )
961
+
962
+ let anonIdGenerator = MyAnonIdGenerator ( )
963
+ var analytics : Analytics ? = Analytics ( configuration: Configuration ( writeKey: " anonIdGenerator " ) . anonymousIdGenerator ( anonIdGenerator) )
964
+ let outputReader = OutputReaderPlugin ( )
965
+ analytics? . add ( plugin: outputReader)
966
+
967
+ waitUntilStarted ( analytics: analytics)
968
+ XCTAssertEqual ( analytics? . anonymousId, " blah-1 " )
969
+
970
+ analytics? . track ( name: " Test1 " )
971
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, " blah-1 " )
972
+ XCTAssertEqual ( anonIdGenerator. currentId, " blah-1 " )
973
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, anonIdGenerator. currentId)
974
+
975
+ analytics? . track ( name: " Test2 " )
976
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, " blah-1 " )
977
+ XCTAssertEqual ( anonIdGenerator. currentId, " blah-1 " )
978
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, anonIdGenerator. currentId)
979
+
980
+ analytics? . reset ( )
981
+
982
+ analytics? . track ( name: " Test3 " )
983
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, " blah-11 " )
984
+ XCTAssertEqual ( anonIdGenerator. currentId, " blah-11 " )
985
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, anonIdGenerator. currentId)
986
+
987
+ analytics? . identify ( userId: " Roger " )
988
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, " blah-11 " )
989
+ XCTAssertEqual ( anonIdGenerator. currentId, " blah-11 " )
990
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, anonIdGenerator. currentId)
991
+
992
+ analytics? . reset ( )
993
+
994
+ analytics? . screen ( title: " Screen " )
995
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, " blah-111 " )
996
+ XCTAssertEqual ( anonIdGenerator. currentId, " blah-111 " )
997
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, anonIdGenerator. currentId)
998
+
999
+ // get rid of this instance, leave it time to go away ...
1000
+ // ... also let any state updates happen as handlers get called async
1001
+ RunLoop . main. run ( until: . distantPast)
1002
+ analytics = nil
1003
+ // ... give it some time to release all it's stuff.
1004
+ RunLoop . main. run ( until: . distantPast)
1005
+
1006
+ // make sure it makes it to the next instance
1007
+ analytics = Analytics ( configuration: Configuration ( writeKey: " anonIdGenerator " ) . anonymousIdGenerator ( anonIdGenerator) )
1008
+ analytics? . add ( plugin: outputReader)
1009
+
1010
+ waitUntilStarted ( analytics: analytics)
1011
+
1012
+ // same anonId as last time, yes?
1013
+ analytics? . screen ( title: " Screen " )
1014
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, " blah-111 " )
1015
+ XCTAssertEqual ( anonIdGenerator. currentId, " blah-111 " )
1016
+ XCTAssertEqual ( outputReader. lastEvent? . anonymousId, anonIdGenerator. currentId)
1017
+
1018
+ }
924
1019
}
0 commit comments