Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
Fix ugly config type surfaced in Api
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Oct 10, 2018
1 parent 3d28b70 commit b27b22b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
24 changes: 12 additions & 12 deletions src/CallPolly/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,18 @@ module Http =
{ timeout: TimeSpan option; sla: TimeSpan option
``base``: Uri option; rel: Uri option
reqLog: Log; resLog: Log }
member __.EffectiveUri : Uri option =
match __.``base``, __.rel with
| None, None -> None
| None, u | u, None -> u
| Some b, Some r ->
let baseWithTrailingSlash =
let current = string b
if current.EndsWith "/" then b
else Uri(current+"/", UriKind.Absolute)
Uri(baseWithTrailingSlash,r) |> Some

let private fold (xs : Rule seq): Configuration * Uri option =
let private fold (xs : Rule seq): Configuration =
let folder s = function
| Rule.BaseUri uri -> { s with ``base`` = Some uri }
| Rule.RelUri uri -> { s with rel = Some uri }
Expand All @@ -124,15 +134,5 @@ module Http =
{ reqLog = Log.Never; resLog = Log.Never
timeout = None; sla = None
``base`` = None; rel = None }
let config = Seq.fold folder def xs
let effectiveAddress =
match config.``base``, config.rel with
| None, u | u, None -> u
| Some b, Some r ->
let baseWithTrailingSlash =
let current = string b
if current.EndsWith "/" then b
else Uri(current+"/", UriKind.Absolute)
Uri(baseWithTrailingSlash,r) |> Some
config, effectiveAddress
Seq.fold folder def xs
let ofInputs xs = xs |> Seq.collect interpret |> fold
7 changes: 3 additions & 4 deletions tests/CallPolly.Acceptance/Orchestration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,12 @@ type Scenarios(output : Xunit.Abstractions.ITestOutputHelper) =
}

let [<Fact>] ``Trapping - Arbitrary Polly expressions can be used to define a failure condition`` () = async {
let selectPolicy (cfg: CallPolly.Rules.CallConfig<Config.Http.Configuration*Uri option>) =
let config,effectiveUri = cfg.config
let selectPolicy (cfg: CallPolly.Rules.CallConfig<Config.Http.Configuration>) =
Polly.Policy
.Handle<TimeoutException>()
.Or<BadGatewayException>(fun _e ->
effectiveUri |> Option.exists (fun (u : Uri) -> (string u).Contains "upstreamb")
&& config.reqLog = Config.Http.Log.Always)
cfg.config.EffectiveUri |> Option.exists (fun (u : Uri) -> (string u).Contains "upstreamb")
&& cfg.config.reqLog = Config.Http.Log.Always)
let policy = Parser.parse(policy).CreatePolicy(log, selectPolicy)
let sut = Sut(log, policy)
let! r = Seq.replicate 9 Succeed |> Seq.map sut.ApiTenSecondSla |> Async.Parallel
Expand Down
2 changes: 1 addition & 1 deletion tests/CallPolly.Tests/ParsingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ type ConfigParsing(output : Xunit.Abstractions.ITestOutputHelper) =
}}}"""

let res = Parser.parse(defs).CreatePolicy log
let effectiveUri = trap <@ res.TryFind("svc","call").Value.Config |> snd |> Option.get @>
let effectiveUri = trap <@ res.TryFind("svc","call").Value.Config.EffectiveUri |> Option.get @>
test <@ "https://base/api/call" = string effectiveUri @>
10 changes: 4 additions & 6 deletions tests/CallPolly.Tests/RulesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,15 @@ module Core =
let baseParsed = mkHttp <| Config.Http.Input.Value.Uri { ``base``=Some (string baseUri); path=None }
let logParsed = mkHttp <| Config.Http.Input.Value.Log { req=Config.Http.Input.LogLevel.OnlyWhenDebugEnabled; res=Config.Http.Input.LogLevel.OnlyWhenDebugEnabled }

let defConfig : Config.Http.Configuration * Uri option =
let defConfig : Config.Http.Configuration =
{ timeout = Some (s 5); sla = Some (s 1)
``base`` = Some baseUri; rel = None
reqLog = Config.Http.Log.OnlyWhenDebugEnabled; resLog = Config.Http.Log.OnlyWhenDebugEnabled },
Some baseUri
reqLog = Config.Http.Log.OnlyWhenDebugEnabled; resLog = Config.Http.Log.OnlyWhenDebugEnabled }
let noPolicy = { isolate = false; cutoff = None; limit = None; breaker = None; }
let heavyConfig : Config.Http.Configuration * Uri option =
let heavyConfig : Config.Http.Configuration =
{ timeout = Some (s 10); sla = Some (s 5)
``base`` = None; rel = None
reqLog = Config.Http.Log.OnlyWhenDebugEnabled; resLog = Config.Http.Log.OnlyWhenDebugEnabled },
None
reqLog = Config.Http.Log.OnlyWhenDebugEnabled; resLog = Config.Http.Log.OnlyWhenDebugEnabled }
let heavyRules = { isolate = true; cutoff = Some cutoffConfig; limit = Some limitConfig; breaker = Some breakConfig }
let mkParsedSla sla timeout = Parser.ParsedRule.Http (Config.Http.Input.Value.Sla {slaMs = sla; timeoutMs = timeout })

Expand Down

0 comments on commit b27b22b

Please sign in to comment.