Skip to content
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
4 changes: 2 additions & 2 deletions DemoApp/REES46Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
) -> Bool {

// Under the tests the demo initializes nothing and shows an empty screen: no test reaches a
// server, and the first initialization of a process decides its core (`Rees46Config.coreV2`),
// which is the tests' to choose.
// server, and the first initialization of a process decides its core — the old one for an app,
// the new one for a bridge's `Rees46.initializeBridge` —, which is the tests' to choose.
if Self.isRunningTests {
window?.rootViewController = UIViewController()
return true
Expand Down
78 changes: 72 additions & 6 deletions DemoApp/Rees46DemoTests/CoreGapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,14 @@ final class CoreGapTests: CoreScenarioTestCase {
let heard = Locked<[Rees46.SdkError?]>([])
let done = expectation(description: "init")
done.expectedFulfillmentCount = 2
let config = Rees46Config(shopId: shop, apiDomain: "stub.test", autoSendPushToken: false, coreV2: coreV2)
let sdk = Rees46.initialize(config, completion: Rees46.BridgeErrors.completion { error in heard.mutate { $0.append(error) }; done.fulfill() })
let config = Rees46Config(shopId: shop, apiDomain: "stub.test", autoSendPushToken: false)
let start: (@escaping (LegacySdkError?) -> Void) -> PersonalizationSDK = { completion in
coreV2 ? Rees46.initializeBridge(config, completion: completion) : Rees46.initialize(config, completion: completion)
}
let sdk = start(Rees46.BridgeErrors.completion { error in heard.mutate { $0.append(error) }; done.fulfill() })
if coreV2 {
// The same shop again: its outcome, once more.
_ = Rees46.initialize(config, completion: Rees46.BridgeErrors.completion { error in heard.mutate { $0.append(error) }; done.fulfill() })
_ = start(Rees46.BridgeErrors.completion { error in heard.mutate { $0.append(error) }; done.fulfill() })
} else {
(sdk as? SimplePersonalizationSDK)?.whenInitializedForTests { error in heard.mutate { $0.append(error) }; done.fulfill() }
}
Expand All @@ -775,6 +778,69 @@ final class CoreGapTests: CoreScenarioTestCase {
}
}

// MARK: - The bridges' way in: the new core and the A/B group they kept

private func bridgeConfig(_ shop: String) -> Rees46Config {
Rees46Config(shopId: shop, apiDomain: "stub.test", autoSendPushToken: false)
}

func test_initialize_bridge_runs_the_new_core_and_an_apps_initialize_the_old_one() {
CoreFlag.reset()
let bridged = newShop()
let sdk = Rees46.initializeBridge(bridgeConfig(bridged)) as? SimplePersonalizationSDK
XCTAssertEqual(sdk?.usesCoreV2, true)
XCTAssertEqual(sdk?.core.kind, .v2)
XCTAssertEqual(CoreFlag.current, true, "the bridge decided the process's core")
sdk?.release()

CoreFlag.reset()
let app = newShop()
let legacy = Rees46.initialize(bridgeConfig(app)) as? SimplePersonalizationSDK
XCTAssertEqual(legacy?.usesCoreV2, false, "an app's configuration runs the old core")
XCTAssertEqual(legacy?.core.kind, .legacy)
XCTAssertEqual(legacy?.config.coreV2, false)

// A bridge after the app: the process keeps the core the app decided, and says so.
let late = newShop()
let after = Rees46.initializeBridge(bridgeConfig(late)) as? SimplePersonalizationSDK
XCTAssertEqual(after?.usesCoreV2, false)
XCTAssertTrue(waitForLog(late, "core_flag_mismatch"))
XCTAssertEqual(CoreFlag.current, false)
legacy?.release()
after?.release()
}

func test_initialize_bridge_brings_its_ab_group_only_to_a_device_that_has_none() {
let deviceSuite = UserDefaults(suiteName: DeviceStore.suiteName) ?? .standard
let before = deviceSuite.string(forKey: DeviceStore.abGroupKey)
defer { deviceSuite.set(before, forKey: DeviceStore.abGroupKey) }
deviceSuite.removeObject(forKey: DeviceStore.abGroupKey)
CoreFlag.reset()

let first = newShop()
let a = Rees46.initializeBridge(bridgeConfig(first), segment: "B")
XCTAssertEqual(a.segment, "B", "the device had no group: it takes the bridge's")
XCTAssertEqual(DeviceStore.shared.abGroup, "B")
XCTAssertEqual(StoragePartition.store(for: first).string(forKey: StoragePartition.abSegmentKey), "B", "and the shop, for the old core")
XCTAssertTrue(logged(first, "invalid_config").isEmpty)

let second = newShop()
let b = Rees46.initializeBridge(bridgeConfig(second), segment: "A")
XCTAssertEqual(b.segment, "B", "the device keeps its own")
XCTAssertTrue(waitForLog(second, "invalid_config", reason: "segment_kept"))

let third = newShop()
let c = Rees46.initializeBridge(bridgeConfig(third), segment: "a")
XCTAssertEqual(c.segment, "B", "only a group the SDK makes is taken")
XCTAssertTrue(waitForLog(third, "invalid_config", reason: "segment_invalid"))

// The shop's instance is there: its group stays, whatever the bridge brings.
XCTAssertTrue((Rees46.initializeBridge(bridgeConfig(first), segment: "A") as AnyObject) === (a as AnyObject))
XCTAssertTrue(waitForLog(first, "invalid_config", reason: "segment_kept"))
XCTAssertEqual(DeviceStore.shared.abGroup, "B")
[a, b, c].forEach { $0.release() }
}

func test_queue_event_of_a_refused_or_released_instance_fails_on_main_and_queues_nothing() {
let shop = newShop()
answer("/init", StubReply(403))
Expand Down Expand Up @@ -1006,10 +1072,10 @@ final class CoreGapTests: CoreScenarioTestCase {
func test_the_process_keeps_its_core_after_a_release_and_a_pending_shop_of_the_other_core_takes_the_process_one() {
CoreFlag.reset()
let first = newShop()
let a = Rees46.initialize(Rees46Config(shopId: first, apiDomain: "stub.test", autoSendPushToken: false, coreV2: true))
let a = Rees46.initializeBridge(Rees46Config(shopId: first, apiDomain: "stub.test", autoSendPushToken: false))
a.release()
let second = newShop()
let b = Rees46.initialize(Rees46Config(shopId: second, apiDomain: "stub.test", autoSendPushToken: false, coreV2: false))
let b = Rees46.initialize(Rees46Config(shopId: second, apiDomain: "stub.test", autoSendPushToken: false))
XCTAssertEqual((b as? SimplePersonalizationSDK)?.usesCoreV2, true, "decided by the first, until the process ends")
XCTAssertTrue(waitForLog(second, "core_flag_mismatch"))
b.release()
Expand All @@ -1020,7 +1086,7 @@ final class CoreGapTests: CoreScenarioTestCase {
let tracked = Locked<[PendingPush]>([])
Rees46.pendingPushTracker = { push in tracked.mutate { $0.append(push) } }
Rees46.register(shops: [
Rees46Config(shopId: legacy, apiDomain: "stub.test", coreV2: false),
Rees46Config(shopId: legacy, apiDomain: "stub.test"),
Rees46Config(shopId: pending, apiDomain: "stub.test", coreV2: true),
])
StoragePartition.store(for: pending).set("d", forKey: StoragePartition.deviceIdKey)
Expand Down
6 changes: 3 additions & 3 deletions DemoApp/Rees46DemoTests/CoreNetworkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//

import XCTest
@testable import REES46
@_spi(Bridge) @testable import REES46

final class CoreNetworkTests: XCTestCase {

Expand Down Expand Up @@ -451,7 +451,7 @@ final class CoreNetworkTests: XCTestCase {
let legacy = createPersonalizationSDK(shopId: shop, apiDomain: "stub.test", autoSendPushToken: false)
XCTAssertEqual(CoreFlag.current, false)

let later = Rees46.initialize(Rees46Config(shopId: "\(shop)-2", apiDomain: "stub.test", autoSendPushToken: false, coreV2: true))
let later = Rees46.initializeBridge(Rees46Config(shopId: "\(shop)-2", apiDomain: "stub.test", autoSendPushToken: false))
XCTAssertEqual((later as? SimplePersonalizationSDK)?.usesCoreV2, false, "the process keeps the old core")
withExtendedLifetime([legacy, later] as [AnyObject]) {}
StubSDK.removeSavedInit(shopId: shop)
Expand All @@ -460,7 +460,7 @@ final class CoreNetworkTests: XCTestCase {

func test_the_old_core_is_the_default() {
XCTAssertFalse(Rees46Config(shopId: "a").coreV2)
XCTAssertTrue(Rees46Config(shopId: "a", coreV2: true).coreV2)
XCTAssertTrue(Rees46Config(shopId: "a").onNewCore().coreV2, "the bridges' configuration")
let ready = expectation(description: "init")
let sdk = StubSDK.make(shopId: shopId) { _ in ready.fulfill() }
wait(for: [ready], timeout: 5)
Expand Down
10 changes: 5 additions & 5 deletions DemoApp/Rees46DemoTests/LegacyParityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// LegacyParityTests.swift
// REES46Tests
//
// The old core (`Rees46Config.coreV2 = false`) behaves as 3.37.3 did, but for the fixes named for both
// The old core (an app's `Rees46.initialize`) behaves as 3.37.3 did, but for the fixes named for both
// cores: a repeated `initialize` after a failed `/init` or with `needReInitialization`, the request
// queue the app sees while `/init` is on its way and the order its requests leave in, the push token it
// reads, `enableLogs`, the tracking and push wire, the decoding rules, the error types of `/init`, the
Expand Down Expand Up @@ -81,10 +81,10 @@ final class LegacyParityTests: CoreScenarioTestCase {
CoreFlag.reset(to: true)
let v2 = newShop()
answer("/init", StubReply(500))
let v2Config = Rees46Config(shopId: v2, apiDomain: "stub.test", autoSendPushToken: false, coreV2: true)
let made = Rees46.initialize(v2Config)
XCTAssertTrue(same(Rees46.initialize(v2Config), made), "the new core repeats its /init itself")
XCTAssertTrue(same(Rees46.initialize(Rees46Config(shopId: v2, apiDomain: "stub.test", autoSendPushToken: false, needReInitialization: true, coreV2: true)), made))
let v2Config = Rees46Config(shopId: v2, apiDomain: "stub.test", autoSendPushToken: false)
let made = Rees46.initializeBridge(v2Config)
XCTAssertTrue(same(Rees46.initializeBridge(v2Config), made), "the new core repeats its /init itself")
XCTAssertTrue(same(Rees46.initializeBridge(Rees46Config(shopId: v2, apiDomain: "stub.test", autoSendPushToken: false, needReInitialization: true)), made))
made.release()
}

Expand Down
5 changes: 3 additions & 2 deletions REES46/Classes/Core/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
//
// The engine under one SDK instance. Two of them run side by side while the new one is built:
// `LegacyCore` — the request paths the SDK has always had — and `CoreV2`, the core of
// `sdk/core/*.md`, behind `Rees46Config.coreV2`. The SDK's request funnel (`sendGet`,
// `postRequest`, `getRequest`) asks the instance's core, so no namespace is written twice.
// `sdk/core/*.md`, behind `Rees46Config.coreV2`, which only the bridges' `Rees46.initializeBridge`
// sets. The SDK's request funnel (`sendGet`, `postRequest`, `getRequest`) asks the instance's core,
// so no namespace is written twice.
//

import Foundation
Expand Down
8 changes: 4 additions & 4 deletions REES46/Classes/Core/CoreV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// CoreV2.swift
// REES46
//
// The new core, behind `Rees46Config.coreV2`: the requests of the funnel go through the new client
// (`NetworkClient`), `/init` is the new core's own (`InitMachine`) and so is the session
// (`SessionWindow`): a call waits until the instance is usable, at most its own timeout, and goes with
// the current `sid`.
// The new core, behind `Rees46Config.coreV2` (`Rees46.initializeBridge`): the requests of the funnel
// go through the new client (`NetworkClient`), `/init` is the new core's own (`InitMachine`) and so
// is the session (`SessionWindow`): a call waits until the instance is usable, at most its own
// timeout, and goes with the current `sid`.
//

import Foundation
Expand Down
35 changes: 29 additions & 6 deletions REES46/Classes/Sdk/Facade/Rees46+Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ import Foundation
@_spi(Bridge)
extension Rees46 {

/**
`initialize(_:completion:)` on the new core: the bridges' way in, the only one to that core. It
makes or finds the shop's instance as `initialize` does, with the same `completion`. The core is
still the process's, decided by its first `initialize`, `initializeBridge` or `register`: after an
app's own `initialize` or `register` the instance runs the old core, and the log says
`core_flag_mismatch`.

`segment` is the device's A/B group the bridge kept in its own storage from before — React Native
kept one per shop. The device takes it only while it has no group of its own, neither in the
device's store nor in the shop's partition; otherwise its own stays and the log says
`invalid_config` with the reason `segment_kept`. Only a group the SDK makes itself — `A` or `B` —
is taken: another is refused, the log says `invalid_config` with the reason `segment_invalid`, and
the device keeps or gets its own as without one. `nil` or empty is none.
*/
@discardableResult
public static func initializeBridge(
_ config: Rees46Config,
segment: String? = nil,
completion: ((LegacySdkError?) -> Void)? = nil
) -> PersonalizationSDK {
initialize(config.onNewCore(), segment: segment, completion: completion)
}

/// `listener` hears every push event the SDK handles, of every shop, on the main thread, beside the
/// app's `PushCallbacks` and whatever they are: once per event of a message, like them, with the
/// shop the push went to in `PushPayload.shopId`. Until the handle is cancelled.
Expand Down Expand Up @@ -88,9 +111,9 @@ extension Rees46 {
extension PersonalizationSDK {

/// Calls `listener` on the main thread with every new session id (`sid`) until the handle is
/// cancelled, so a bridge tells its app instead of asking for `sessionId` on a timer. With
/// `Rees46Config.coreV2` each `sid` an `/init` brings; on the old core the one
/// `deleteUserCredentials()` makes. Another conformer of the protocol tells nothing.
/// cancelled, so a bridge tells its app instead of asking for `sessionId` on a timer. On the new
/// core each `sid` an `/init` brings; on the old core the one `deleteUserCredentials()` makes.
/// Another conformer of the protocol tells nothing.
@discardableResult
public func onSessionChange(_ listener: @escaping (String) -> Void) -> Cancellable {
guard let instance = self as? SimplePersonalizationSDK else { return .noop }
Expand All @@ -115,7 +138,7 @@ extension PersonalizationSDK {
goes — the device's token changed — hears the answer of the newer one.

The old `setPushTokenNotification` keeps telling the app at once that the token waits for the
device id, as it always has; on `Rees46Config.coreV2` it, too, hears the answer once it went.
device id, as it always has; on the new core it, too, hears the answer once it went.
*/
public func setPushToken(_ token: Rees46.PushToken, completion: @escaping (Result<Void, Rees46.SdkError>) -> Void) {
let finish: (Result<Void, LegacySdkError>) -> Void = { result in
Expand Down Expand Up @@ -149,8 +172,8 @@ extension PersonalizationSDK {

`completion` hears once, on the main thread: success once the event is accepted — on disk, the way
the contract's tracking methods complete —, or `invalidArgument` for an event of a push, another
path or a body that is not JSON, `notInitialized`, `shopBlocked`. With `Rees46Config.coreV2 =
false`, which keeps no queue, the event goes at once and `completion` hears the server's answer.
path or a body that is not JSON, `notInitialized`, `shopBlocked`. On the old core, which keeps no
queue, the event goes at once and `completion` hears the server's answer.
*/
public func queueEvent(path: String, body: [String: Any], completion: @escaping (Result<Void, Rees46.SdkError>) -> Void) {
let finish: (Rees46.SdkError?) -> Void = { error in
Expand Down
Loading
Loading