Skip to content

Commit eba56b5

Browse files
committed
Fix tests by always lifting null-checking internal function outside of taskSeq or task CE, or it may not fire in time
1 parent f88816d commit eba56b5

File tree

2 files changed

+41
-62
lines changed

2 files changed

+41
-62
lines changed

src/FSharp.Control.TaskSeq/TaskSeq.fs

Lines changed: 40 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -236,47 +236,39 @@ module TaskSeq =
236236

237237
let tryHead source = Internal.tryHead source
238238

239-
let head source = task {
240-
match! Internal.tryHead source with
241-
| Some head -> return head
242-
| None -> return Internal.raiseEmptySeq ()
243-
}
239+
let head source =
240+
Internal.tryHead source
241+
|> Task.map (Option.defaultWith Internal.raiseEmptySeq)
244242

245243
let tryLast source = Internal.tryLast source
246244

247-
let last source = task {
248-
match! Internal.tryLast source with
249-
| Some last -> return last
250-
| None -> return Internal.raiseEmptySeq ()
251-
}
245+
let last source =
246+
Internal.tryLast source
247+
|> Task.map (Option.defaultWith Internal.raiseEmptySeq)
252248

253249
let tryTail source = Internal.tryTail source
254250

255-
let tail source = task {
256-
match! Internal.tryTail source with
257-
| Some result -> return result
258-
| None -> return Internal.raiseEmptySeq ()
259-
}
251+
let tail source =
252+
Internal.tryTail source
253+
|> Task.map (Option.defaultWith Internal.raiseEmptySeq)
260254

261255
let tryItem index source = Internal.tryItem index source
262256

263-
let item index source = task {
264-
match! Internal.tryItem index source with
265-
| Some item -> return item
266-
| None ->
267-
if index < 0 then
268-
return invalidArg (nameof index) "The input must be non-negative."
269-
else
270-
return Internal.raiseInsufficient ()
271-
}
257+
let item index source =
258+
if index < 0 then
259+
invalidArg (nameof index) "The input must be non-negative."
260+
261+
Internal.tryItem index source
262+
|> Task.map (Option.defaultWith Internal.raiseInsufficient)
272263

273264
let tryExactlyOne source = Internal.tryExactlyOne source
274265

275-
let exactlyOne source = task {
276-
match! Internal.tryExactlyOne source with
277-
| Some item -> return item
278-
| None -> return invalidArg (nameof source) "The input sequence contains more than one element."
279-
}
266+
let exactlyOne source =
267+
Internal.tryExactlyOne source
268+
|> Task.map (
269+
Option.defaultWith (fun () ->
270+
invalidArg (nameof source) "The input sequence contains more than one element.")
271+
)
280272

281273
let indexed (source: taskSeq<'T>) =
282274
Internal.checkNonNull (nameof source) source
@@ -318,47 +310,34 @@ module TaskSeq =
318310
Internal.tryFind (Predicate((=) value)) source
319311
|> Task.map (Option.isSome)
320312

321-
let pick chooser source = task {
322-
match! Internal.tryPick (TryPick chooser) source with
323-
| Some item -> return item
324-
| None -> return Internal.raiseNotFound ()
325-
}
313+
let pick chooser source =
314+
Internal.tryPick (TryPick chooser) source
315+
|> Task.map (Option.defaultWith Internal.raiseNotFound)
326316

327-
let pickAsync chooser source = task {
328-
match! Internal.tryPick (TryPickAsync chooser) source with
329-
| Some item -> return item
330-
| None -> return Internal.raiseNotFound ()
331-
}
332-
333-
let find predicate source = task {
334-
match! Internal.tryFind (Predicate predicate) source with
335-
| Some item -> return item
336-
| None -> return Internal.raiseNotFound ()
337-
}
317+
let pickAsync chooser source =
318+
Internal.tryPick (TryPickAsync chooser) source
319+
|> Task.map (Option.defaultWith Internal.raiseNotFound)
338320

321+
let find predicate source =
322+
Internal.tryFind (Predicate predicate) source
323+
|> Task.map (Option.defaultWith Internal.raiseNotFound)
339324

340-
let findAsync predicate source = task {
341-
match! Internal.tryFind (PredicateAsync predicate) source with
342-
| Some item -> return item
343-
| None -> return Internal.raiseNotFound ()
344-
}
325+
let findAsync predicate source =
326+
Internal.tryFind (PredicateAsync predicate) source
327+
|> Task.map (Option.defaultWith Internal.raiseNotFound)
345328

346-
let findIndex predicate source = task {
347-
match! Internal.tryFindIndex (Predicate predicate) source with
348-
| Some item -> return item
349-
| None -> return Internal.raiseNotFound ()
350-
}
329+
let findIndex predicate source =
330+
Internal.tryFindIndex (Predicate predicate) source
331+
|> Task.map (Option.defaultWith Internal.raiseNotFound)
351332

352-
let findIndexAsync predicate source = task {
353-
match! Internal.tryFindIndex (PredicateAsync predicate) source with
354-
| Some item -> return item
355-
| None -> return Internal.raiseNotFound ()
356-
}
333+
let findIndexAsync predicate source =
334+
Internal.tryFindIndex (PredicateAsync predicate) source
335+
|> Task.map (Option.defaultWith Internal.raiseNotFound)
357336

358337

359338

360339
//
361-
// zip/unzip etc functions
340+
// zip/unzip/fold etc functions
362341
//
363342

364343
let zip source1 source2 = Internal.zip source1 source2

src/FSharp.Control.TaskSeq/TaskSeq.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ module TaskSeq =
323323
val tryItem: index: int -> source: taskSeq<'T> -> Task<'T option>
324324

325325
/// <summary>
326-
/// Returns the nth element of the <see cref="taskSeq" />, or <see cref="None" /> if the sequence
326+
/// Returns the nth element of the <see cref="taskSeq" />, or raises an exception if the sequence
327327
/// does not contain enough elements, or if <paramref name="index" /> is negative.
328328
/// </summary>
329329
/// <exception cref="ArgumentException">Thrown when the sequence has insufficient length or

0 commit comments

Comments
 (0)