<commit_before>﻿namespace Nessos.MBrace.Tests

open System
open System.IO
open System.Threading

open NUnit.Framework
open FsUnit

open Nessos.Thespian

open Nessos.MBrace
open Nessos.MBrace.SampleRuntime

[<TestFixture>]
module ``Distribution Tests`` =

    type Canceller = Actors.EventActor<unit>

    let mutable runtime : MBraceRuntime option = None

    [<TestFixtureSetUp>]
    let init () =
        MBraceRuntime.WorkerExecutable <- Path.Combine(__SOURCE_DIRECTORY__, "..", "..", "bin", "MBrace.SampleRuntime.exe")
        runtime <- Some <| MBraceRuntime.InitLocal(4)

    [<TestFixtureTearDown>]
    let fini () =
        runtime |> Option.iter (fun r -> r.Kill())
        runtime <- None

    let run (workflow : Cloud<'T>) = Option.get(runtime).RunAsync workflow |> Async.Catch |> Async.RunSynchronously
    let runCts (workflow : Canceller -> Cloud<'T>) =
        let cts = new CancellationTokenSource()
        let observable, canceller = Canceller.Init()
        let _ = observable.Subscribe(fun () -> cts.Cancel())
        Option.get(runtime).RunAsync(workflow canceller, cancellationToken = cts.Token) |> Async.Catch |> Async.RunSynchronously

    [<Test>]
    let ``Parallel : empty input`` () =
        run (Cloud.Parallel [||]) |> Choice.shouldEqual [||]

    [<Test>]
    let ``Parallel : simple inputs`` () =
        cloud {
            do! Cloud.Sleep 20000
            let f i = cloud { return i + 1 }
            let! results = Array.init 100 f |> Cloud.Parallel
            return Array.sum results
        } |> run |> Choice.shouldEqual 5050

    [<Test>]
    let ``Parallel : simple nested`` () =
        cloud {
            let f i j = cloud { return i + j + 2 }
            let cluster i = Array.init 10 (f i) |> Cloud.Parallel
            let! results = Array.init 10 cluster |> Cloud.Parallel<commit_msg>remote sleep in unit test<commit_after>﻿namespace Nessos.MBrace.Tests

open System
open System.IO
open System.Threading

open NUnit.Framework
open FsUnit

open Nessos.Thespian

open Nessos.MBrace
open Nessos.MBrace.SampleRuntime

[<TestFixture>]
module ``Distribution Tests`` =

    type Canceller = Actors.EventActor<unit>

    let mutable runtime : MBraceRuntime option = None

    [<TestFixtureSetUp>]
    let init () =
        MBraceRuntime.WorkerExecutable <- Path.Combine(__SOURCE_DIRECTORY__, "..", "..", "bin", "MBrace.SampleRuntime.exe")
        runtime <- Some <| MBraceRuntime.InitLocal(4)

    [<TestFixtureTearDown>]
    let fini () =
        runtime |> Option.iter (fun r -> r.Kill())
        runtime <- None

    let run (workflow : Cloud<'T>) = Option.get(runtime).RunAsync workflow |> Async.Catch |> Async.RunSynchronously
    let runCts (workflow : Canceller -> Cloud<'T>) =
        let cts = new CancellationTokenSource()
        let observable, canceller = Canceller.Init()
        let _ = observable.Subscribe(fun () -> cts.Cancel())
        Option.get(runtime).RunAsync(workflow canceller, cancellationToken = cts.Token) |> Async.Catch |> Async.RunSynchronously

    [<Test>]
    let ``Parallel : empty input`` () =
        run (Cloud.Parallel [||]) |> Choice.shouldEqual [||]

    [<Test>]
    let ``Parallel : simple inputs`` () =
        cloud {
            let f i = cloud { return i + 1 }
            let! results = Array.init 100 f |> Cloud.Parallel
            return Array.sum results
        } |> run |> Choice.shouldEqual 5050

    [<Test>]
    let ``Parallel : simple nested`` () =
        cloud {
            let f i j = cloud { return i + j + 2 }
            let cluster i = Array.init 10 (f i) |> Cloud.Parallel
            let! results = Array.init 10 cluster |> Cloud.Parallel
{'fraction_non_alphanumeric': 0.12767123287671234, 'fraction_numerical': 0.00904109589041096, 'mean_word_length': 2.6328358208955223, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3944855', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1127, 'n_words': 292}