Skip to content

Commit f60532e

Browse files
committed
Tidy infra
1 parent 63fd19b commit f60532e

11 files changed

Lines changed: 36 additions & 238 deletions

File tree

src/Equinox.Core/Infrastructure.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
module internal Equinox.Core.Infrastructure
44

55
open FSharp.Control
6-
open System
76
open System.Diagnostics
87
open System.Threading.Tasks
98

tests/Equinox.CosmosStore.Integration/Equinox.CosmosStore.Integration.fsproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
<ItemGroup>
2828
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.17.0" />
29-
<!-- <PackageReference Include="Microsoft.Azure.Cosmos.Direct" Version="3.17.1" />-->
3029
<PackageReference Include="FsCheck.xUnit" Version="2.14.0" />
3130
<PackageReference Include="JsonDiffPatch.Net" Version="2.1.0" />
3231
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />

tools/Equinox.Tool/Equinox.Tool.fsproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
<ItemGroup>
2323
<Compile Include="Infrastructure\Infrastructure.fs" />
24-
<Compile Include="Infrastructure\HttpHelpers.fs" />
2524
<Compile Include="StoreClient.fs" />
2625
<Compile Include="TodoClient.fs" />
2726
<Compile Include="Tests.fs" />
@@ -40,7 +39,6 @@
4039
<PackageReference Include="MinVer" Version="2.5.0" PrivateAssets="All" />
4140

4241
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.17.0" />
43-
<!-- <PackageReference Include="Microsoft.Azure.Cosmos.Direct" Version="3.17.1" />-->
4442

4543
<!-- NOTE cannot be 4.7.0 as Async.Sequential is broken-->
4644
<PackageReference Include="FSharp.Core" Version="4.7.1" />

tools/Equinox.Tool/Infrastructure/HttpHelpers.fs

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 15 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[<AutoOpen>]
2-
module Equinox.Tool.Infrastructure.Prelude
2+
module internal Equinox.Tool.Infrastructure.Prelude
33

4+
open Equinox.Tools.TestHarness.HttpHelpers
45
open System
56
open System.Diagnostics
67
open System.Text
7-
open System.Threading
88

99
type Exception with
1010
// https://github.com/fsharp/fslang-suggestions/issues/660
11-
member this.Reraise () =
11+
member this.Reraise() =
1212
(System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture this).Throw ()
1313
Unchecked.defaultof<_>
1414

@@ -17,7 +17,7 @@ type Async with
1717
/// Raises an exception using Async's continuation mechanism directly.
1818
/// </summary>
1919
/// <param name="exn">Exception to be raised.</param>
20-
static member Raise (exn : #exn) = Async.FromContinuations(fun (_,ec,_) -> ec exn)
20+
static member Raise(exn : #exn) = Async.FromContinuations(fun (_,ec,_) -> ec exn)
2121

2222
/// <summary>
2323
/// Gets the result of given task so that in the event of exception
@@ -33,7 +33,7 @@ type Async with
3333
let e = t.Exception
3434
if e.InnerExceptions.Count = 1 then ec e.InnerExceptions.[0]
3535
else ec e
36-
elif t.IsCanceled then ec(new System.Threading.Tasks.TaskCanceledException())
36+
elif t.IsCanceled then ec(System.Threading.Tasks.TaskCanceledException())
3737
else sc t.Result)
3838
|> ignore)
3939
[<DebuggerStepThrough>]
@@ -49,40 +49,12 @@ type Async with
4949
else
5050
sc ())
5151
|> ignore)
52-
/// Creates an async computation which runs the provided sequence of computations and completes
53-
/// when all computations in the sequence complete. Up to parallelism computations will
54-
/// be in-flight at any given point in time. Error or cancellation of any computation in
55-
/// the sequence causes the resulting computation to error or cancel, respectively.
56-
/// Like Async.Parallel but with support for throttling.
57-
/// Note that an array is allocated to contain the results of all computations.
58-
static member ParallelThrottled (parallelism:int) (tasks:seq<Async<'T>>) : Async<'T[]> = async {
59-
if parallelism < 1 then invalidArg "parallelism" "Must be positive number."
60-
use semaphore = new SemaphoreSlim(parallelism)
61-
let throttledWorker (task:Async<'T>) = async {
62-
let! ct = Async.CancellationToken
63-
do! semaphore.WaitAsync ct |> Async.AwaitTaskCorrect
64-
try return! task
65-
finally ignore(semaphore.Release())
66-
}
67-
68-
return! tasks |> Seq.map throttledWorker |> Async.Parallel
69-
}
70-
71-
type StringBuilder with
72-
member sb.Appendf fmt = Printf.ksprintf (ignore << sb.Append) fmt
73-
member sb.Appendfn fmt = Printf.ksprintf (ignore << sb.AppendLine) fmt
74-
75-
static member inline Build(builder : StringBuilder -> unit) =
76-
let instance = StringBuilder() // TOCONSIDER PooledStringBuilder.GetInstance()
77-
builder instance
78-
instance.ToString()
7952

8053
[<AutoOpen>]
8154
module HttpHelpers =
8255

8356
open System.Net
8457
open System.Net.Http
85-
open System.Runtime.Serialization
8658

8759
/// Operations on System.Net.HttpRequestMessage
8860
module HttpReq =
@@ -138,25 +110,12 @@ module HttpHelpers =
138110
request.Headers.Add(name, value)
139111
request
140112

141-
type HttpContent with
142-
member c.ReadAsString() = async {
143-
match c with
144-
| null -> return null
145-
| c -> return! c.ReadAsStringAsync() |> Async.AwaitTaskCorrect
146-
}
147-
148-
// only intended for logging under control of InvalidHttpResponseException, hence the esoteric name
149-
member internal c.ReadAsStringDiapered() = async {
150-
try return! c.ReadAsString()
151-
with :? ObjectDisposedException -> return "<HttpContent:ObjectDisposedException>"
152-
}
153-
154113
type HttpClient with
155114
/// <summary>
156115
/// Drop-in replacement for HttpClient.SendAsync which addresses known timeout issues
157116
/// </summary>
158117
/// <param name="msg">HttpRequestMessage to be submitted.</param>
159-
member client.Send(msg : HttpRequestMessage) = async {
118+
member client.SendAsync2(msg : HttpRequestMessage) = async {
160119
let! ct = Async.CancellationToken
161120
try return! client.SendAsync(msg, ct) |> Async.AwaitTaskCorrect
162121
// address https://github.com/dotnet/corefx/issues/20296
@@ -169,67 +128,6 @@ module HttpHelpers =
169128
return! Async.Raise(TimeoutException message)
170129
}
171130

172-
/// Exception indicating an unexpected response received by an Http Client
173-
type InvalidHttpResponseException =
174-
inherit Exception
175-
176-
// TODO: include headers
177-
val private userMessage : string
178-
val private requestMethod : string
179-
val RequestUri : Uri
180-
val RequestBody : string
181-
val StatusCode : HttpStatusCode
182-
val ReasonPhrase : string
183-
val ResponseBody : string
184-
185-
member __.RequestMethod = new HttpMethod(__.requestMethod)
186-
187-
private new (userMessage : string, requestMethod : HttpMethod, requestUri : Uri, requestBody : string,
188-
statusCode : HttpStatusCode, reasonPhrase : string, responseBody : string,
189-
?innerException : exn) =
190-
{
191-
inherit Exception(message = null, innerException = defaultArg innerException null) ; userMessage = userMessage ;
192-
requestMethod = string requestMethod ; RequestUri = requestUri ; RequestBody = requestBody ;
193-
StatusCode = statusCode ; ReasonPhrase = reasonPhrase ; ResponseBody = responseBody
194-
}
195-
196-
override e.Message =
197-
StringBuilder.Build(fun sb ->
198-
sb.Appendfn "%s %O RequestUri=%O HttpStatusCode=%O" e.userMessage e.RequestMethod e.RequestUri e.StatusCode
199-
let getBodyString str = if String.IsNullOrWhiteSpace str then "<null>" else str
200-
sb.Appendfn "RequestBody=%s" (getBodyString e.RequestBody)
201-
sb.Appendfn "ResponseBody=%s" (getBodyString e.ResponseBody))
202-
203-
interface ISerializable with
204-
member e.GetObjectData(si : SerializationInfo, sc : StreamingContext) =
205-
let add name (value:obj) = si.AddValue(name, value)
206-
base.GetObjectData(si, sc) ; add "userMessage" e.userMessage ;
207-
add "requestUri" e.RequestUri ; add "requestMethod" e.requestMethod ; add "requestBody" e.RequestBody
208-
add "statusCode" e.StatusCode ; add "reasonPhrase" e.ReasonPhrase ; add "responseBody" e.ResponseBody
209-
210-
new (si : SerializationInfo, sc : StreamingContext) =
211-
let get name = si.GetValue(name, typeof<'a>) :?> 'a
212-
{
213-
inherit Exception(si, sc) ; userMessage = get "userMessage" ;
214-
RequestUri = get "requestUri" ; requestMethod = get "requestMethod" ; RequestBody = get "requestBody" ;
215-
StatusCode = get "statusCode" ; ReasonPhrase = get "reasonPhrase" ; ResponseBody = get "responseBody"
216-
}
217-
218-
static member Create(userMessage : string, response : HttpResponseMessage, ?innerException : exn) = async {
219-
let request = response.RequestMessage
220-
let! responseBodyC = response.Content.ReadAsStringDiapered() |> Async.StartChild
221-
let! requestBody = request.Content.ReadAsStringDiapered()
222-
let! responseBody = responseBodyC
223-
return
224-
new InvalidHttpResponseException(
225-
userMessage, request.Method, request.RequestUri, requestBody,
226-
response.StatusCode, response.ReasonPhrase, responseBody,
227-
?innerException = innerException)
228-
}
229-
230-
static member Create(response : HttpResponseMessage, ?innerException : exn) =
231-
InvalidHttpResponseException.Create("HTTP request yielded unexpected response.", response, ?innerException = innerException)
232-
233131
type HttpResponseMessage with
234132

235133
/// Raises an <c>InvalidHttpResponseException</c> if the response status code does not match expected value.
@@ -264,4 +162,12 @@ module HttpHelpers =
264162

265163
/// Deserialize body using default Json.Net profile - throw with content details if StatusCode is not OK or decoding fails
266164
let deserializeOkJsonNet<'t> =
267-
deserializeExpectedJsonNet<'t> HttpStatusCode.OK
165+
deserializeExpectedJsonNet<'t> HttpStatusCode.OK
166+
167+
type StringBuilder with
168+
member sb.Appendf fmt = Printf.ksprintf (ignore << sb.Append) fmt
169+
170+
static member inline Build(builder : StringBuilder -> unit) =
171+
let instance = StringBuilder() // TOCONSIDER PooledStringBuilder.GetInstance()
172+
builder instance
173+
instance.ToString()

tools/Equinox.Tool/StoreClient.fs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ open System.Net.Http
88

99
type Session(client: HttpClient, clientId: ClientId) =
1010

11-
member __.Send(req : HttpRequestMessage) : Async<HttpResponseMessage> =
11+
member _.Send(req : HttpRequestMessage) : Async<HttpResponseMessage> =
1212
let req = req |> HttpReq.withHeader "COMPLETELY_INSECURE_CLIENT_ID" (ClientId.toString clientId)
13-
client.Send(req)
13+
client.SendAsync2(req)
1414

15-
type Favorited = { date: System.DateTimeOffset; skuId: SkuId }
15+
type Favorited = { date: DateTimeOffset; skuId: SkuId }
1616

1717
type FavoritesClient(session: Session) =
1818

19-
member __.Favorite(skus: SkuId[]) = async {
19+
member _.Favorite(skus: SkuId[]) = async {
2020
let request = HttpReq.post () |> HttpReq.withPath "api/favorites" |> HttpReq.withJsonNet skus
2121
let! response = session.Send request
2222
return! response.EnsureStatusCode(HttpStatusCode.NoContent)
2323
}
2424

25-
member __.List = async {
25+
member _.List = async {
2626
let request = HttpReq.get () |> HttpReq.withPath "api/favorites"
2727
let! response = session.Send request
2828
return! response |> HttpRes.deserializeOkJsonNet<Favorited[]>
@@ -33,8 +33,8 @@ type Saved = { skuId : SkuId; dateSaved : DateTimeOffset }
3333
type SavesClient(session: Session) =
3434

3535
// this (returning a bool indicating whether it got saved) is fine for now
36-
// IRL we don't want to be leaning on the fact we get a 400 when we exceed the max imems limit as a core API design element
37-
member __.Save(skus: SkuId[]) : Async<bool> = async {
36+
// IRL we don't want to be leaning on the fact we get a 400 when we exceed the max items limit as a core API design element
37+
member _.Save(skus: SkuId[]) : Async<bool> = async {
3838
let request = HttpReq.post () |> HttpReq.withPath "api/saves" |> HttpReq.withJsonNet skus
3939
let! response = session.Send request
4040
if response.StatusCode = HttpStatusCode.BadRequest then
@@ -44,13 +44,13 @@ type SavesClient(session: Session) =
4444
return true
4545
}
4646

47-
member __.Remove(skus: SkuId[]) : Async<unit> = async {
47+
member _.Remove(skus: SkuId[]) : Async<unit> = async {
4848
let request = HttpReq.delete () |> HttpReq.withPath "api/saves" |> HttpReq.withJsonNet skus
4949
let! response = session.Send request
5050
return! response.EnsureStatusCode(HttpStatusCode.NoContent)
5151
}
5252

53-
member __.List = async {
53+
member _.List = async {
5454
let request = HttpReq.get () |> HttpReq.withPath "api/saves"
5555
let! response = session.Send request
5656
return! response |> HttpRes.deserializeOkJsonNet<Saved[]>
@@ -59,4 +59,4 @@ type SavesClient(session: Session) =
5959
type Session with
6060

6161
member session.Favorites = FavoritesClient session
62-
member session.Saves = SavesClient session
62+
member session.Saves = SavesClient session

tools/Equinox.Tool/TodoClient.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ type Todo = { id: int; url: string; order: int; title: string; completed: bool }
1010

1111
type Session(client: HttpClient, clientId: ClientId) =
1212

13-
member __.Send(req : HttpRequestMessage) : Async<HttpResponseMessage> =
13+
member _.Send(req : HttpRequestMessage) : Async<HttpResponseMessage> =
1414
let req = req |> HttpReq.withHeader "COMPLETELY_INSECURE_CLIENT_ID" (ClientId.toString clientId)
15-
client.Send(req)
15+
client.SendAsync2(req)
1616

1717
type TodosClient(session: Session) =
1818

@@ -37,7 +37,7 @@ type TodosClient(session: Session) =
3737
}
3838

3939
member __.Clear() : Async<unit> = async {
40-
let request = HttpReq.delete () |> HttpReq.withPath basePath
40+
let request = HttpReq.delete () |> HttpReq.withPath basePath
4141
let! response = session.Send request
4242
return! response.EnsureStatusCode(HttpStatusCode.NoContent)
4343
}

tools/Equinox.Tools.TestHarness/Equinox.Tools.TestHarness.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
<PackageReference Include="MinVer" Version="2.5.0" PrivateAssets="All" />
2222
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
2323

24-
<PackageReference Include="FSharp.Core" Version="4.3.4" />
24+
<!-- 4.7.0 has broken Async.Sequential, Async.Parallel with degree of parallelism parameter -->
25+
<PackageReference Include="FSharp.Core" Version="4.7.1" />
2526

2627
<PackageReference Include="MathNet.Numerics" Version="4.7.0" />
2728
<PackageReference Include="Serilog" Version="2.7.1" />

0 commit comments

Comments
 (0)