private async Task closeChannel() { var messages = Channel <int> .CreateNonBuffered(); Go.Run(async() => { try { while (true) { Console.WriteLine("waiting..."); var msg = await messages.RecvAsync(); Console.WriteLine(msg); } } catch (ChannelClosedException) { Console.WriteLine("ChannelClosedException caught"); } }); for (int i = 0; i < 10; i++) { await messages.SendAsync(i); } messages.Close(); Console.WriteLine("closed"); }
static void Main() { var foregroundColor = Console.ForegroundColor; var backgroundColor = Console.BackgroundColor; try { Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.Black; Console.WriteLine(@" ______ ______ ______ ______ __ __ /\ ___\ /\ == \ /\ __ \ /\ == \ /\ \/ / \ \___ \ \ \ _-/ \ \ \/\ \ \ \ __< \ \ _""-. \/\_____\ \ \_\ \ \_____\ \ \_\ \_\ \ \_\ \_\ \/_____/ \/_/ \/_____/ \/_/ /_/ \/_/\/_/ "); Go.Run(); } finally { Console.ForegroundColor = foregroundColor; Console.BackgroundColor = backgroundColor; } }
public static void Main(string[] args) { Console.WriteLine("============================"); Console.WriteLine("= NuGet Package Visualizer ="); Console.WriteLine("============================"); Go.Run <Program>(args); }
private void simpleSelectSync() { var chan1 = Channel <int> .CreateNonBuffered(); var chan2 = Channel <string> .CreateNonBuffered(); Go.Run(() => { Thread.Sleep(TimeSpan.FromSeconds(1)); chan1.Send(1); }); Go.Run(() => { Thread.Sleep(TimeSpan.FromSeconds(2)); chan2.Send("two"); }); for (int i = 0; i < 2; i++) { Select .CaseRecv(chan1, msg1 => Console.WriteLine($"recvd: {msg1}")) .CaseRecv(chan2, msg2 => Console.WriteLine($"recvd: {msg2}")) .Go(); } }
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"); }
private async Task simpleSelect() { var chan1 = Channel <int> .CreateNonBuffered(); var chan2 = Channel <string> .CreateNonBuffered(); Go.Run(async() => { await Task.Delay(TimeSpan.FromSeconds(1)); await chan1.SendAsync(1); }); Go.Run(async() => { await Task.Delay(TimeSpan.FromSeconds(2)); await chan2.SendAsync("two"); }); for (int i = 0; i < 2; i++) { await Select .CaseRecv(chan1, msg1 => Console.WriteLine($"recvd: {msg1}")) .CaseRecv(chan2, msg2 => Console.WriteLine($"recvd: {msg2}")) .GoAsync(); } }
public void ReproduceError() { var errorCode = Go.Run <CommandoWithTwoArguments>(new[] { "/src/somepath", "/src/anotherpath" }); Assert.AreEqual(0, errorCode); Assert.AreEqual("/src/somepath", CommandoWithTwoArguments.SomePathStatic); Assert.AreEqual("/src/anotherpath", CommandoWithTwoArguments.AnotherPathStatic); }
static void Main() { Log.Logger = new LoggerConfiguration() .WriteTo.ColoredConsole(outputTemplate: LogTemplate) .WriteTo.RollingFile(@"C:\logs\zipperblobber\log.txt") .MinimumLevel.Debug() .CreateLogger(); Go.Run(); }
private void SendRecvRunTask(int senderTaskCount, int sendItemCount, int recvTaskCount, int recvItemCount, int?bufferSize = null) { var stopWatch = new Stopwatch(); var channel = !bufferSize.HasValue ? Channel <int> .CreateNonBuffered() : Channel <int> .CreateBuffered(bufferSize.Value); int cntSend = 0; int cntRecv = 0; if (senderTaskCount * sendItemCount != recvTaskCount * recvItemCount) { throw new ArgumentException("Invalid parameterization"); } var cde = new CountdownEvent(recvTaskCount + senderTaskCount); stopWatch.Start(); for (int i = 0; i < recvTaskCount; i++) { Go.Run(async() => { for (int j = 0; j < recvItemCount; j++) { await channel.RecvAsync(); Interlocked.Increment(ref cntRecv); } cde.Signal(); }); } for (int i = 0; i < senderTaskCount; i++) { Go.Run(async() => { for (int j = 0; j < sendItemCount; j++) { await channel.SendAsync(i); Interlocked.Increment(ref cntSend); } ; cde.Signal(); }); } cde.Wait(); stopWatch.Stop(); Console.WriteLine($"Sender tsk: {senderTaskCount}, items/tsk: {sendItemCount}; Recv tsk: {recvTaskCount}, items/tsk: {recvItemCount}: {stopWatch.ElapsedMilliseconds} ms"); if (cntSend != cntRecv || cntSend != senderTaskCount * sendItemCount) { throw new Exception("channel error"); } }
public void ClosingAReceivingChannelCompletesItImmediatelyWithDefault() { // this tests all things in m_queue not in m_promises; we need some other thing for that var ci = new Channel <int>(); Go.Run(async() => { await Task.Delay(25); // let the Receive begin ci.Close(); }); Assert.AreEqual(new ReceivedValue <int>(0, false), ci.ReceiveEx().Result); }
private void sendRecvSync() { var messages = Channel <string> .CreateNonBuffered(); Go.Run(() => { messages.Send("ping"); }); var msg = messages.Recv(); Console.WriteLine(msg); }
public void HowAboutThis() { var errorCode = Go.Run <CommandoWithThreeArguments>(new[] { @"src\Noder.Test\bin\Debug\Noder.Test.dll", @"src\Noder.Test\Features\00_basics.feature" }); Assert.AreEqual(0, errorCode); Assert.AreEqual(@"src\Noder.Test\bin\Debug\Noder.Test.dll", CommandoWithThreeArguments.SomePathStatic); Assert.AreEqual(@"src\Noder.Test\Features\00_basics.feature", CommandoWithThreeArguments.AnotherPathStatic); Assert.IsFalse(CommandoWithThreeArguments.SomeFlagStatic); }
private async Task sendRecv() { var messages = Channel <string> .CreateNonBuffered(); Go.Run(async() => { await messages.SendAsync("ping"); }); var msg = await messages.RecvAsync(); Console.WriteLine(msg); }
public static async Task Run() { var result = new Channel <int>(); var messages = new Channel <string>(); Go.Run(MultiplyAsync, 10, 20, result, messages); WriteLine($"result was {await result.Receive().ConfigureAwait(false)}"); WriteLine($"message was {await messages.Receive().ConfigureAwait(false)}"); result.Receive().Wait(); messages.Receive().Wait(); }
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); }
static void ScanDir(string dir, Channel <CrcResult> results, Channel <Exception> errors, WaitGroup wg) { foreach (var f in Directory.GetFiles(dir)) { var absPath = Path.Combine(dir, f); wg.Add(1); Go.Run(CalcCrc32, absPath, results, errors, wg); } foreach (var d in Directory.GetDirectories(dir)) { var absPath = Path.Combine(dir, d); wg.Add(1); Go.Run(ScanDir, absPath, results, errors, wg); } wg.Done(); }
async void StartListeningOnChannels() { while (m_directoryChanged.IsOpen) { var directoryName = await m_directoryChanged.Receive(); StorageFolder storageFolder; try { storageFolder = await StorageFolder.GetFolderFromPathAsync(directoryName); } catch (Exception) { continue; // invalid folder } var addToList = new Channel <string>(); var dataLoaded = new Channel <FileInfo>(); Go.Run(ScanDirectoryAsync, storageFolder, true, addToList, dataLoaded); m_observableCollection.Clear(); while (true) { var rv = await addToList.ReceiveEx(); if (!rv.IsValid) { break; // channel closed } var fi = new FileInfo { Name = rv.Value }; var insertedIndex = m_observableCollection.Count; m_observableCollection.Add(fi); var rv2 = await dataLoaded.ReceiveEx(); if (!rv2.IsValid) { break; // channel closed } fi.CopyFrom(rv2.Value); await DrawTreeMap(); } } }
private async Task ticker() { var ticker = new Ticker(TimeSpan.FromMilliseconds(100)); Go.Run(() => { foreach (var t in ticker.Range) { Console.WriteLine($"Tick at: {t}"); } }); ticker.Start(); await Task.Delay(TimeSpan.FromSeconds(1)); ticker.Stop(); }
private async Task rangeChannel() { var messages = Channel <int> .CreateNonBuffered(); Go.Run(() => { foreach (var msg in messages.Range) { Console.WriteLine(msg); } }); for (int i = 0; i < 10; i++) { await messages.SendAsync(i); } messages.Close(); }
public async Task ChannelSendRecvTestAsync() { var channel = Channel <int> .CreateNonBuffered(); var done = Channel <bool> .CreateNonBuffered(); int i = 2; Go.Run(async() => { i = await channel.RecvAsync(); await done.SendAsync(true); }); await channel.SendAsync(1); await done.RecvAsync(); Assert.AreEqual(1, i); }
public async Task SelectTestAsync() { var channel = Channel <int> .CreateBuffered(1); var done = Channel <bool> .CreateNonBuffered(); int i = 2; Go.Run(async() => { await Select.CaseRecv(channel, a => i = a).GoAsync(); await Select.CaseSend(done, true).GoAsync(); }); await Select.CaseSend(channel, 1).DefaultAsync(() => i = 3); await Select.CaseRecv(done, _ => { }).GoAsync(); Assert.AreEqual(1, i); }
private async Task chanSync() { var done = Channel <bool> .CreateNonBuffered(); Go.Run(async() => { Console.WriteLine("begin"); await Task.Delay(TimeSpan.FromSeconds(1)); Console.WriteLine("done"); await done.SendAsync(true); }); await done.RecvAsync(); Console.WriteLine("end"); }
private async Task ScanDirectoryAsync(StorageFolder storageFolder, bool doSha1, Channel <string> addToList, Channel <FileInfo> dataLoaded) { using (addToList) using (dataLoaded) { foreach (var entry in await storageFolder.GetItemsAsync()) { var folder = entry as StorageFolder; // folder if (folder != null) { await addToList.Send(folder.Name); var innerAddToList = new Channel <string>(); var innerDataLoaded = new Channel <FileInfo>(); Go.Run(ScanDirectoryAsync, folder, false, innerAddToList, innerDataLoaded); var totalSize = await innerAddToList .Zip(innerDataLoaded, (s, f) => f.Size) .Sum(x => x) .ConfigureAwait(false); await dataLoaded.Send(new FileInfo { Name = folder.Name, Size = totalSize, IsLoaded = true }); } var file = entry as StorageFile; // file if (file != null) { var fi = new FileInfo { Name = file.Name }; await addToList.Send(file.Name); try { fi = await ScanFile(file, doSha1).ConfigureAwait(false); } catch (Exception e) { Debug.WriteLine($"{e} scanning {file.Name}"); } await dataLoaded.Send(fi); // if we fail, publish an empty entry to complete the sequence } } } }
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}"); })); } }
static void Main() { Go.Run(); }
static int Main(string[] args) { return(Go.Run <Program>(args)); }
static void Main() { Go go = new Go(); go.Run(); }
static void Main(string[] args) { Go go = new Go(); go.Run(args); }
static void Main(string[] args) { Go.Run(); }
static void Main(string[] args) { Go.Run <Program>(args); }