scientistproject / Scientist.net

A .NET library for carefully refactoring critical paths. It's a port of GitHub's Ruby Scientist library
MIT License
1.46k stars 95 forks source link

ScienceAsync does not execute Use and Try in parallel #130

Closed dtjason closed 4 years ago

dtjason commented 4 years ago

The documentation indicates that when using Scientist.ScienceAsync the Use and Try functions should run in parallel and should only take as long as the slowest function.

https://github.com/scientistproject/Scientist.net#running-candidates-in-parallel-asynchronous

The following code always takes 10 seconds to run. Additionally, "Done1" and "Done2" seem to complete in a random order each time.

namespace Science
{
    class Program
    {
        static void Main(string[] args)
        {

            var t = Scientist.ScienceAsync<int>("ExperimentName", 2,
                experiment => {
                    experiment.Use(async () => await Thing1());
                    experiment.Try(async () => await Thing2());
                });

            t.Wait();
            Console.WriteLine("All Done");
            Console.ReadLine();
        }

        static async Task<int> Thing1()
        {
            Thread.Sleep(5000);
            Console.WriteLine("Done1");
            return await Task.FromResult(0);
        }

        static async Task<int> Thing2()
        {
            Thread.Sleep(5000);
            Console.WriteLine("Done2");
            return await Task.FromResult(1);
        }
    }
}
dtjason commented 4 years ago

Function implementations appear to require a TaskRun in order to function properly.

M-Zuber commented 4 years ago

Would you feel up to making a pull request to add some documentation around this?