public void Should_Start_At_Most_Limit_Operations_In_Parallel() { var tcsArray = GetTaskCompletionSources <bool>(100); var counter = 0; Func <long, Task> method = index => { Interlocked.Increment(ref counter); return(tcsArray[index % tcsArray.Length].Task); }; var timesTask = ConcurrentUtils.Times(1000, 10, method); Assert.AreEqual(TaskStatus.WaitingForActivation, timesTask.Status); Assert.AreEqual(Volatile.Read(ref counter), 10); for (var i = 0; i < 10; i++) { tcsArray[i].SetResult(true); Assert.AreEqual(Volatile.Read(ref counter), 11 + i); } Assert.AreEqual(TaskStatus.WaitingForActivation, timesTask.Status); for (var i = 10; i < tcsArray.Length; i++) { tcsArray[i].SetResult(true); } Assert.AreEqual(TaskStatus.RanToCompletion, timesTask.Status); Assert.AreEqual(Volatile.Read(ref counter), 1000); }
public async Task Should_Complete_Task_When_All_Tasks_Are_Completed() { var tcs1 = new TaskCompletionSource <bool>(); var tcs2 = new TaskCompletionSource <bool>(); var indexes = new long?[1000]; Func <long, Task> method = index => { indexes[index] = index; if (index == 5) { return(tcs2.Task); } return(tcs1.Task); }; var timesTask = ConcurrentUtils.Times(1000, 10, method); Assert.AreEqual(TaskStatus.WaitingForActivation, timesTask.Status); tcs1.SetResult(true); Assert.AreEqual(TaskStatus.WaitingForActivation, timesTask.Status); await Task.Delay(40); Assert.AreEqual(TaskStatus.WaitingForActivation, timesTask.Status); // Assert that all the indexes where passed starting from 0 for (long i = 0; i < indexes.Length; i++) { Assert.AreEqual(i, indexes[i]); } tcs2.SetResult(true); Assert.AreEqual(TaskStatus.RanToCompletion, timesTask.Status); }
public async Task Should_Map_Async() { var rnd = new Random(); var sourceStrings = new string[] { "Hello", "Hello world", "How are you", "I'm doing fine" }; var source = new string[100]; for (var i = 0; i < source.Length; i++) { source[i] = sourceStrings[i % sourceStrings.Length]; } Func <string, Task <int> > method = async(text) => { await Task.Delay((int)Math.Floor(rnd.NextDouble() * 100)); return(text.Length); }; var mappedValues = await ConcurrentUtils.Map(source, 7, method); Assert.AreEqual(source.Length, mappedValues.Length); for (var i = 0; i < source.Length; i++) { Assert.AreEqual(source[i].Length, mappedValues[i]); } }
public async Task Enqueue_Should_Fault_Task_When_Func_Throws() { var tcsArray = GetTaskCompletionSources <bool>(10); Console.WriteLine("Started"); Func <int, Task> method = index => { if (index == tcsArray.Length / 2) { throw new Exception("Test exception"); } return(tcsArray[index % tcsArray.Length].Task); }; var jobQueue = ConcurrentUtils.CreateQueue(4, method); var tasks = tcsArray.Select((tcs, i) => jobQueue.Enqueue(i)).ToArray(); Assert.AreEqual(tcsArray.Length, jobQueue.Count); for (var i = 0; i < tcsArray.Length; i++) { if (i != tcsArray.Length / 2) { tcsArray[i].SetResult(true); } } await Task.Delay(100); Assert.AreEqual(tcsArray.Length - 1, tasks.Count(t => t.Status == TaskStatus.RanToCompletion)); Assert.AreEqual(1, tasks.Count(t => t.Status == TaskStatus.Faulted)); }
public async Task Enqueue_Should_Complete_Task_When_Job_Is_Completed_According_To_Limit(int limit) { var tcsArray = GetTaskCompletionSources <bool>(50); var started = 0; Func <long, Task> method = index => { Interlocked.Increment(ref started); return(tcsArray[index % tcsArray.Length].Task); }; var jobQueue = ConcurrentUtils.CreateQueue(limit, method); var tasks = tcsArray.Select((tcs, i) => jobQueue.Enqueue(i)).ToArray(); Assert.AreEqual(limit, Volatile.Read(ref started)); Assert.AreEqual(jobQueue.Count, tcsArray.Length); for (var i = 0; i < tcsArray.Length; i++) { tcsArray[i].SetResult(true); await Task.Delay(10); Assert.AreEqual(TaskStatus.RanToCompletion, tasks[i].Status); var expectedStarted = limit + 1 + i; if (expectedStarted > tcsArray.Length) { expectedStarted = tcsArray.Length; } Assert.AreEqual(expectedStarted, Volatile.Read(ref started)); Assert.AreEqual(tcsArray.Length - i - 1, jobQueue.Count); } }
public void Should_Fault_Task_When_Any_Of_The_Tasks_Are_Faulted() { var tcs1 = new TaskCompletionSource <bool>(); var tcs2 = new TaskCompletionSource <bool>(); Func <long, Task> method = index => { if (index == 15) { return(tcs2.Task); } return(tcs1.Task); }; var timesTask = ConcurrentUtils.Times(1000, 10, method); Assert.AreEqual(TaskStatus.WaitingForActivation, timesTask.Status); tcs1.SetResult(true); Assert.AreEqual(TaskStatus.WaitingForActivation, timesTask.Status); var ex = new Exception("Test exception"); tcs2.SetException(ex); Assert.AreEqual(TaskStatus.Faulted, timesTask.Status); Assert.AreEqual(ex, timesTask.Exception.InnerException); }
public async Task Enqueue_Should_Fault_Task_When_Job_Failed() { var tcsArray = GetTaskCompletionSources <bool>(10); Func <long, Task> method = index => tcsArray[index % tcsArray.Length].Task; var jobQueue = ConcurrentUtils.CreateQueue(4, method); var tasks = tcsArray.Select((tcs, i) => jobQueue.Enqueue(i)).ToArray(); Assert.AreEqual(jobQueue.Count, tcsArray.Length); for (var i = 0; i < tcsArray.Length; i++) { if (i != tcsArray.Length / 2) { tcsArray[i].SetResult(true); } else { tcsArray[i].SetException(new Exception("Test exception")); } } await Task.Delay(10); Assert.AreEqual(tcsArray.Length - 1, tasks.Count(t => t.Status == TaskStatus.RanToCompletion)); Assert.AreEqual(1, tasks.Count(t => t.Status == TaskStatus.Faulted)); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void parse() throws org.maltparser.core.exception.MaltChainedException public virtual void parse() { Console.WriteLine("Start parse"); long startTime = System.nanoTime(); IList <string[]> inputSentences = new List <string[]>(); StreamReader reader = null; string infile = OptionManager.instance().getOptionValueString(optionContainer, "input", "infile"); string incharset = OptionManager.instance().getOptionValueString(optionContainer, "input", "charset"); try { reader = new StreamReader(new FileStream(infile, FileMode.Open, FileAccess.Read), incharset); while (true) { string[] tokens = ConcurrentUtils.readSentence(reader); if (tokens.Length == 0) { break; } inputSentences.Add(tokens); } } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } finally { if (reader != null) { try { reader.Close(); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } } } Console.WriteLine(getMessageWithElapsed("Read sentences time", startTime)); int numberOfThreads = 8; Thread[] threads = new Thread[numberOfThreads]; MaltParserRunnable[] runnables = new MaltParserRunnable[numberOfThreads]; int nSentences = inputSentences.Count; int interval = (nSentences / numberOfThreads); int startIndex = 0; int t = 0; Console.WriteLine("Number of sentences : " + nSentences); while (startIndex < nSentences) { int endIndex = (startIndex + interval < nSentences && t < threads.Length - 1?startIndex + interval:nSentences); Console.WriteLine(" Thread " + string.Format("{0:D3}", t) + " will parse sentences between " + string.Format("{0:D4}", startIndex) + " - " + string.Format("{0:D4}", (endIndex - 1)) + ", number of sentences: " + (endIndex - startIndex)); runnables[t] = new MaltParserRunnable(inputSentences.subList(startIndex, endIndex), model); threads[t] = new Thread(runnables[t]); startIndex = endIndex; t++; } Console.WriteLine(getMessageWithElapsed("Create threads time", startTime)); startTime = System.nanoTime(); // Starting threads to parse all sentences. for (int i = 0; i < threads.Length; i++) { if (threads[i] != null) { threads[i].Start(); } else { Console.Error.WriteLine("Thread " + i + " is null"); } } // Finally joining all threads for (int i = 0; i < threads.Length; i++) { try { if (threads[i] != null) { threads[i].Join(); } else { Console.Error.WriteLine("Thread " + i + " is null"); } } catch (InterruptedException) { } } Console.WriteLine(getMessageWithElapsed("Parsing time", startTime)); startTime = System.nanoTime(); string outfile = OptionManager.instance().getOptionValueString(optionContainer, "output", "outfile"); string outcharset = OptionManager.instance().getOptionValueString(optionContainer, "output", "charset"); StreamWriter writer = null; try { writer = new StreamWriter(new FileStream(outfile, FileMode.Create, FileAccess.Write), outcharset); for (int i = 0; i < threads.Length; i++) { IList <string[]> outputSentences = runnables[i].OutputSentences; for (int j = 0; j < outputSentences.Count; j++) { ConcurrentUtils.writeSentence(outputSentences[j], writer); } } } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } finally { if (writer != null) { try { writer.Close(); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } } } Console.WriteLine(getMessageWithElapsed("Write sentences time", startTime)); }