Пример #1
0
    public async Task MultiChannels_SelectCanShareLambdas()
    {
        var c1 = new Channel <int>();
        var c2 = new Channel <int>();
        var c3 = new Channel <string>();

        var sb = new StringBuilder();

        var t1 = c1.Send(1); // don't wait for the sends to complete, they won't until later
        var t2 = c2.Send(2);
        var t3 = c3.Send("3");

        // it frees up after the first time it's hit
        await Go.Select(
            Go.Case(new[] { c1, c2 }, i => sb.Append($"cx={i};")),
            Go.Case(c3, i => sb.Append($"c3={i};")));

        Assert.AreEqual("cx=1;", sb.ToString());

        await Go.Select(
            Go.Case(new[] { c1, c2 }, i => sb.Append($"cx={i};")),
            Go.Case(c3, i => sb.Append($"c3={i};")));

        Assert.AreEqual("cx=1;cx=2;", sb.ToString());

        await Go.Select(
            Go.Case(new[] { c1, c2 }, i => sb.Append($"cx={i};")),
            Go.Case(c3, i => sb.Append($"c3={i};")));

        Assert.AreEqual("cx=1;cx=2;c3=3;", sb.ToString());
    }
Пример #2
0
    public async Task MultiChannels_SelectPullsFromEachChannel()
    {
        var c1 = new Channel <int>();
        var c2 = new Channel <int>();

        var sb = new StringBuilder();

        var t = Task.Run(async() => {
            await c1.Send(1);
            await c2.Send(2);
        });

        // it frees up after the first time it's hit
        await Go.Select(
            Go.Case(c1, i => sb.Append($"c1={i};")),
            Go.Case(c2, i => sb.Append($"c2={i};")));

        Assert.AreEqual("c1=1;", sb.ToString());

        await Go.Select(
            Go.Case(c1, i => sb.Append($"c1={i};")),
            Go.Case(c2, i => sb.Append($"c2={i};")));

        Assert.AreEqual("c1=1;c2=2;", sb.ToString());
    }
Пример #3
0
    public static async Task Run()
    {
        var results = new Channel <CrcResult>();
        var errors  = new Channel <Exception>();
        var wg      = new WaitGroup();

        wg.Add(1);

        Go.Run(async() => {  // close the channels when the waitGroup signals
            await wg.Wait();
            results.Close();
            errors.Close();
        });
        Go.Run(ScanDir, "/Users/orion/OneDrive/Ignite2015/dev/goroutines", results, errors, wg);

        int totalFiles = 0;

        while (results.IsOpen || errors.IsOpen)
        {
            await Go.Select(
                Go.Case(results, r => {
                Console.WriteLine($"Got {r.Value} for {r.Path}");
                totalFiles++;
            }),
                Go.Case(errors, exception => {
                Console.WriteLine($"EXCEPTION: {exception}");
                totalFiles++;
            }));
        }

        Console.WriteLine($"{totalFiles} total files");
    }
Пример #4
0
    public void SelectOnClosedChannelReturnsImmediatelyWithoutSignalling()
    {
        var ci   = new Channel <int>(); ci.Close();
        var hits = new List <int>();

        Go.Select(
            Go.Case(ci, hits.Add)).Wait(); // if the channel doesn't complete our unit test will hang and we'll find out the hard way

        Assert.AreEqual(0, hits.Count);
    }
Пример #5
0
    public async Task SelectIgnoresNullChannels()
    {
        var ci = new Channel <int>();
        var t1 = ci.Send(1);

        var hits = new List <object>();
        await Go.Select(
            Go.Case(ci, i => hits.Add(i)),
            Go.Case((Channel <string>)null, s => hits.Add(s)));

        Assert.AreEqual(1, hits.Count);
        Assert.AreEqual(1, hits[0]);
    }
Пример #6
0
    public void ClosingASelectedChannelCompletesItImmediatelySignallingChannelClosed()
    {
        var ci   = new Channel <int>();
        var hits = new List <Tuple <int, bool> >();

        Go.Run(async() => {
            await Task.Delay(25); // let the Receive begin
            ci.Close();
        });
        Go.Select(
            Go.Case(ci, (v, ok) => hits.Add(Tuple.Create(v, ok)))).Wait(); // if the channel doesn't complete our unit test will hang and we'll find out the hard way

        CollectionAssert.AreEqual(new[] { Tuple.Create(0, false) }, hits);
    }
Пример #7
0
    public async Task MultiChannels_SelectDoesntLoseUnselectedValues()
    {
        var c1 = new Channel <int>();
        var c2 = new Channel <int>();

        var sb = new StringBuilder();

        var t1 = c1.Send(1); // don't wait for the sends to complete, they won't until later
        var t2 = c2.Send(2);

        // it frees up after the first time it's hit
        await Go.Select(
            Go.Case(c1, i => sb.Append($"c1={i};")),
            Go.Case(c2, i => sb.Append($"c2={i};")));

        Assert.AreEqual("c1=1;", sb.ToString());

        await Go.Select(
            Go.Case(c1, i => sb.Append($"c1={i};")),
            Go.Case(c2, i => sb.Append($"c2={i};")));

        Assert.AreEqual("c1=1;c2=2;", sb.ToString());
    }
Пример #8
0
    public static async Task FanOutIn()
    {
        var numbers = new Channel <int>();
        var letters = new Channel <char>();

        Go.Run(async() => {
            for (int i = 0; i < 10; i++)
            {
                await numbers.Send(i);
            }

            Console.WriteLine("numbers all sent");
            numbers.Close();
        });

        Go.Run(async() => {
            for (int i = 0; i < 10; i++)
            {
                await letters.Send((char)(i + 97));
            }

            Console.WriteLine("letters all sent");
            letters.Close();
        });

        while (numbers.IsOpen || letters.IsOpen)
        {
            await Go.Select(
                Go.Case(numbers, num => {
                Console.WriteLine($"Got {num}");
            }),
                Go.Case(letters, ch => {
                Console.WriteLine($"Got {ch}");
            }));
        }
    }