Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from spotify/smaller-fixes
Browse files Browse the repository at this point in the history
Smaller fixes and cleanups
  • Loading branch information
fabriziodemaria authored Mar 31, 2023
2 parents cbd1a0d + 752495d commit 0faccc3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 75 deletions.
12 changes: 6 additions & 6 deletions Sources/OpenFeature/OpenFeatureAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
/// Configuration here will be shared across all ``Client``s.
public class OpenFeatureAPI {
private var _provider: FeatureProvider?
private var _evaluationContext: EvaluationContext = MutableContext()
private var _context: EvaluationContext = MutableContext()
private(set) var hooks: [AnyHook] = []

/// The ``OpenFeatureAPI`` singleton
Expand All @@ -18,12 +18,12 @@ public class OpenFeatureAPI {
}

public func setProvider(provider: FeatureProvider, initialContext: EvaluationContext?) async {
await provider.initialize(initialContext: initialContext ?? self._evaluationContext)
await provider.initialize(initialContext: initialContext ?? self._context)
self._provider = provider
guard let newEvaluationContext = initialContext else {
return
}
self._evaluationContext = newEvaluationContext
self._context = newEvaluationContext
}

public func getProvider() -> FeatureProvider? {
Expand All @@ -35,16 +35,16 @@ public class OpenFeatureAPI {
}

public func setEvaluationContext(evaluationContext: EvaluationContext) async {
await getProvider()?.onContextSet(oldContext: self._evaluationContext, newContext: evaluationContext)
await getProvider()?.onContextSet(oldContext: self._context, newContext: evaluationContext)
// A provider evaluation reading the global ctx at this point would fail due to stale cache.
// To prevent this, the provider should internally manage the ctx to use on each evaluation, and
// make sure it's aligned with the values in the cache at all times. If no guarantees are offered by
// the provider, the application can expect STALE resolves while setting a new global ctx
self._evaluationContext = evaluationContext
self._context = evaluationContext
}

public func getEvaluationContext() -> EvaluationContext? {
return self._evaluationContext
return self._context
}

public func getProviderMetadata() -> Metadata? {
Expand Down
4 changes: 2 additions & 2 deletions Tests/OpenFeatureTests/HookSpecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import XCTest
@testable import OpenFeature

final class HookSpecTests: XCTestCase {
func testNoErrorHookCalled() {
OpenFeatureAPI.shared.clearProvider()
func testNoErrorHookCalled() async {
await OpenFeatureAPI.shared.setProvider(provider: NoOpProvider())
let client = OpenFeatureAPI.shared.getClient()

let hook = BooleanHookMock()
Expand Down
15 changes: 0 additions & 15 deletions Tests/OpenFeatureTests/HookSupportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,3 @@ final class HookSupportTests: XCTestCase {
XCTAssertEqual(hook.errorCalled, 1)
}
}
extension HookSupportTests {
class StringHookMock: StringHook {
private var value: EvaluationContext

init(key: String, value: String) {
let ctx = MutableContext()
ctx.add(key: key, value: .string(value))
self.value = ctx
}

public func before(ctx: HookContext<String>, hints: [String: Any]) -> EvaluationContext? {
return value
}
}
}
52 changes: 0 additions & 52 deletions Tests/OpenFeatureTests/OpenFeatureClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,4 @@ extension OpenFeatureClientTests {
numCalls += 1
}
}

class TestProvider: FeatureProvider {
var hooks: [OpenFeature.AnyHook] = []
var metadata: OpenFeature.Metadata = TestMetadata()
private var targetingKey: String

func onContextSet(oldContext: OpenFeature.EvaluationContext, newContext: OpenFeature.EvaluationContext) {
// no-op
}

func initialize(initialContext: OpenFeature.EvaluationContext) {
// no-op
}

init(targetingKey: String) {
self.targetingKey = targetingKey
}

func getBooleanEvaluation(key: String, defaultValue: Bool) throws
-> OpenFeature.ProviderEvaluation<Bool>
{
return ProviderEvaluation(value: true)
}

func getStringEvaluation(key: String, defaultValue: String) throws
-> OpenFeature.ProviderEvaluation<String>
{
return ProviderEvaluation(value: "")
}

func getIntegerEvaluation(key: String, defaultValue: Int64) throws
-> OpenFeature.ProviderEvaluation<Int64>
{
return ProviderEvaluation(value: 0)
}

func getDoubleEvaluation(key: String, defaultValue: Double) throws
-> OpenFeature.ProviderEvaluation<Double>
{
return ProviderEvaluation(value: 0.0)
}

func getObjectEvaluation(key: String, defaultValue: OpenFeature.Value)
throws -> OpenFeature.ProviderEvaluation<OpenFeature.Value>
{
return ProviderEvaluation(value: .null)
}
}

public struct TestMetadata: Metadata {
public var name: String? = "test"
}
}

0 comments on commit 0faccc3

Please sign in to comment.