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

Feat: add Mutli-instance write-key check #293

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions Sources/Segment/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ extension Analytics {
Self.firstInstance = self
}
}

// is firstInstance nil? If so, set it.
if Self.firstInstance == nil {
Self.firstInstance = self
Expand Down
20 changes: 20 additions & 0 deletions Sources/Segment/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public enum OperatingMode {
// MARK: - Internal Configuration

public class Configuration {

internal let writeKeyLock: NSLock = NSLock()
static var activeWriteKeys: [String?] = []

internal struct Values {
var writeKey: String
var application: Any? = nil
Expand Down Expand Up @@ -59,6 +63,8 @@ public class Configuration {
])

self.defaultSettings(settings)

checkActiveWriteKeys(writeKey: writeKey)
}
}

Expand Down Expand Up @@ -233,6 +239,20 @@ public extension Configuration {
JSON.jsonNonConformingNumberStrategy = values.jsonNonConformingNumberStrategy
return self
}

func checkActiveWriteKeys(writeKey: String) {
writeKeyLock.lock()

defer {
writeKeyLock.unlock()
}

if Configuration.activeWriteKeys.contains(writeKey) {
fatalError("Cannot initialize multiple instances of Analytics with the same write key")
} else {
Configuration.activeWriteKeys.append(writeKey)
}
}
}

extension Analytics {
Expand Down
52 changes: 26 additions & 26 deletions Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class Analytics_Tests: XCTestCase {
}

func testPluginConfigure() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test1"))
let ziggy = ZiggyPlugin()
let myDestination = MyDestination()
let goober = GooberPlugin()
Expand All @@ -31,7 +31,7 @@ final class Analytics_Tests: XCTestCase {
}

func testPluginRemove() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test2"))
let myDestination = MyDestination()
myDestination.add(plugin: GooberPlugin())

Expand Down Expand Up @@ -60,13 +60,13 @@ final class Analytics_Tests: XCTestCase {
return true
}

var settings = Settings(writeKey: "test")
var settings = Settings(writeKey: "test3")
if let existing = settings.integrations?.dictionaryValue {
var newIntegrations = existing
newIntegrations[myDestination.key] = true
settings.integrations = try! JSON(newIntegrations)
}
let configuration = Configuration(writeKey: "test")
let configuration = Configuration(writeKey: "test4")
configuration.defaultSettings(settings)
let analytics = Analytics(configuration: configuration)

Expand Down Expand Up @@ -104,13 +104,13 @@ final class Analytics_Tests: XCTestCase {
return true
}

var settings = Settings(writeKey: "test")
var settings = Settings(writeKey: "test5")
if let existing = settings.integrations?.dictionaryValue {
var newIntegrations = existing
newIntegrations[myDestination.key] = true
settings.integrations = try! JSON(newIntegrations)
}
let configuration = Configuration(writeKey: "test")
let configuration = Configuration(writeKey: "test6")
configuration.defaultSettings(settings)
let analytics = Analytics(configuration: configuration)

Expand Down Expand Up @@ -139,7 +139,7 @@ final class Analytics_Tests: XCTestCase {
return true
}

let configuration = Configuration(writeKey: "test")
let configuration = Configuration(writeKey: "test7")
let analytics = Analytics(configuration: configuration)

analytics.add(plugin: myDestination)
Expand All @@ -155,15 +155,15 @@ final class Analytics_Tests: XCTestCase {
#endif

func testAnonymousId() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test8"))
let anonId = analytics.anonymousId

XCTAssertTrue(anonId != "")
XCTAssertTrue(anonId.count == 36) // it's a UUID y0.
}

func testContext() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test9"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand Down Expand Up @@ -211,7 +211,7 @@ final class Analytics_Tests: XCTestCase {


func testContextWithUserAgent() {
let configuration = Configuration(writeKey: "test")
let configuration = Configuration(writeKey: "test10")
configuration.userAgent("testing user agent")
let analytics = Analytics(configuration: configuration)
let outputReader = OutputReaderPlugin()
Expand Down Expand Up @@ -257,7 +257,7 @@ final class Analytics_Tests: XCTestCase {
}

func testDeviceToken() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test11"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand All @@ -274,7 +274,7 @@ final class Analytics_Tests: XCTestCase {

#if os(iOS) || os(tvOS) || os(visionOS) || targetEnvironment(macCatalyst)
func testDeviceTokenData() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test12"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand All @@ -292,7 +292,7 @@ final class Analytics_Tests: XCTestCase {
#endif

func testTrack() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test13"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand All @@ -306,7 +306,7 @@ final class Analytics_Tests: XCTestCase {
}

func testIdentify() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test14"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand All @@ -321,7 +321,7 @@ final class Analytics_Tests: XCTestCase {
}

func testUserIdAndTraitsPersistCorrectly() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test15"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand All @@ -347,7 +347,7 @@ final class Analytics_Tests: XCTestCase {


func testScreen() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test16"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand All @@ -369,7 +369,7 @@ final class Analytics_Tests: XCTestCase {
}

func testGroup() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test17"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand All @@ -389,7 +389,7 @@ final class Analytics_Tests: XCTestCase {
}

func testReset() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test25"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand Down Expand Up @@ -437,7 +437,7 @@ final class Analytics_Tests: XCTestCase {
}

func testEnabled() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test19"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand Down Expand Up @@ -479,7 +479,7 @@ final class Analytics_Tests: XCTestCase {
}

func testSetFlushAtAfter() {
let analytics = Analytics(configuration: Configuration(writeKey: "1234"))
let analytics = Analytics(configuration: Configuration(writeKey: "12345"))
let countPolicy = CountBasedFlushPolicy(count: 23)
analytics.add(flushPolicy: countPolicy)

Expand Down Expand Up @@ -540,7 +540,7 @@ final class Analytics_Tests: XCTestCase {
}

func testVersion() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test18"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand Down Expand Up @@ -571,7 +571,7 @@ final class Analytics_Tests: XCTestCase {

// Test to ensure bundled and unbundled integrations are populated correctly
func testDestinationMetadata() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test20"))
let mixpanel = AnyDestination(key: "Mixpanel")
let outputReader = OutputReaderPlugin()

Expand Down Expand Up @@ -610,7 +610,7 @@ final class Analytics_Tests: XCTestCase {

// Test to ensure bundled and active integrations are populated correctly
func testDestinationMetadataUnbundled() {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test21"))
let mixpanel = AnyDestination(key: "Mixpanel")
let outputReader = OutputReaderPlugin()

Expand Down Expand Up @@ -721,7 +721,7 @@ final class Analytics_Tests: XCTestCase {
let dead = Analytics.shared()
XCTAssertTrue(dead.isDead)

let alive = Analytics(configuration: Configuration(writeKey: "1234"))
let alive = Analytics(configuration: Configuration(writeKey: "123456"))
XCTAssertFalse(alive.isDead)

let shared = Analytics.shared()
Expand Down Expand Up @@ -826,7 +826,7 @@ final class Analytics_Tests: XCTestCase {

func testJSONNaNDefaultHandlingZero() throws {
// notice we didn't set the nan handling option. zero is the default.
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
let analytics = Analytics(configuration: Configuration(writeKey: "test22"))
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

Expand All @@ -842,7 +842,7 @@ final class Analytics_Tests: XCTestCase {
}

func testJSONNaNHandlingNull() throws {
let analytics = Analytics(configuration: Configuration(writeKey: "test")
let analytics = Analytics(configuration: Configuration(writeKey: "test23")
.jsonNonConformingNumberStrategy(.null)
)
let outputReader = OutputReaderPlugin()
Expand Down
6 changes: 3 additions & 3 deletions Tests/Segment-Tests/FlushPolicy_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FlushPolicyTests: XCTestCase {
}

func testRemoveFlushPolicy() {
let analytics = Analytics(configuration: Configuration(writeKey: "flushPolicyAddTest"))
let analytics = Analytics(configuration: Configuration(writeKey: "flushPolicyAddTest1"))

let dummy = DummyFlushPolicy()
analytics.add(flushPolicy: dummy)
Expand All @@ -72,7 +72,7 @@ class FlushPolicyTests: XCTestCase {
}

func testRemoveAllFlushPolicies() {
let analytics = Analytics(configuration: Configuration(writeKey: "flushPolicyAddTest"))
let analytics = Analytics(configuration: Configuration(writeKey: "flushPolicyAddTest2"))
var policies = analytics.configuration.values.flushPolicies

waitUntilStarted(analytics: analytics)
Expand All @@ -87,7 +87,7 @@ class FlushPolicyTests: XCTestCase {
}

func testFindFlushPolicy() {
let analytics = Analytics(configuration: Configuration(writeKey: "flushPolicyAddTest"))
let analytics = Analytics(configuration: Configuration(writeKey: "flushPolicyAddTest3"))

waitUntilStarted(analytics: analytics)

Expand Down
4 changes: 2 additions & 2 deletions Tests/Segment-Tests/MemoryLeak_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class MemoryLeak_Tests: XCTestCase {
}

func testLeaksVerbose() throws {
let analytics = Analytics(configuration: Configuration(writeKey: "1234"))
let analytics = Analytics(configuration: Configuration(writeKey: "memory_test_verbose"))

waitUntilStarted(analytics: analytics)
analytics.track(name: "test")
Expand Down Expand Up @@ -95,7 +95,7 @@ final class MemoryLeak_Tests: XCTestCase {
}

func testLeaksSimple() throws {
let analytics = Analytics(configuration: Configuration(writeKey: "1234"))
let analytics = Analytics(configuration: Configuration(writeKey: "memory_leak_test_simple"))

waitUntilStarted(analytics: analytics)
analytics.track(name: "test")
Expand Down
14 changes: 7 additions & 7 deletions Tests/Segment-Tests/ObjC_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ObjC_Tests: XCTestCase {
*/

func testWrapping() {
let a = Analytics(configuration: Configuration(writeKey: "WRITE_KEY"))
let a = Analytics(configuration: Configuration(writeKey: "WRITE_KEY0"))
let objc = ObjCAnalytics(wrapping: a)

XCTAssertTrue(objc.analytics === a)
Expand All @@ -47,7 +47,7 @@ class ObjC_Tests: XCTestCase {

func testNonTrivialAnalytics() {
Storage.hardSettingsReset(writeKey: "WRITE_KEY")
let config = ObjCConfiguration(writeKey: "WRITE_KEY")
let config = ObjCConfiguration(writeKey: "WRITE_KEY1")
config.defaultSettings = ["integrations": ["Amplitude": true]]

let analytics = ObjCAnalytics(configuration: config)
Expand All @@ -72,7 +72,7 @@ class ObjC_Tests: XCTestCase {
}

func testTraitsAndUserIdOptionality() {
let config = ObjCConfiguration(writeKey: "WRITE_KEY")
let config = ObjCConfiguration(writeKey: "WRITE_KEY2")
let analytics = ObjCAnalytics(configuration: config)
analytics.reset()

Expand All @@ -90,9 +90,9 @@ class ObjC_Tests: XCTestCase {
var sourceHit: Bool = false
var destHit: Bool = false

Storage.hardSettingsReset(writeKey: "WRITE_KEY")
Storage.hardSettingsReset(writeKey: "WRITE_KEY2")

let config = ObjCConfiguration(writeKey: "WRITE_KEY")
let config = ObjCConfiguration(writeKey: "WRITE_KEY3")
let analytics = ObjCAnalytics(configuration: config)
analytics.analytics.storage.hardReset(doYouKnowHowToUseThis: true)

Expand Down Expand Up @@ -132,9 +132,9 @@ class ObjC_Tests: XCTestCase {
}

func testObjCDictionaryPassThru() {
Storage.hardSettingsReset(writeKey: "WRITE_KEY2")
Storage.hardSettingsReset(writeKey: "WRITE_KEY3")

let config = ObjCConfiguration(writeKey: "WRITE_KEY2")
let config = ObjCConfiguration(writeKey: "WRITE_KEY4")
let analytics = ObjCAnalytics(configuration: config)
analytics.analytics.storage.hardReset(doYouKnowHowToUseThis: true)

Expand Down
Loading
Loading