public void Check_if_data_is_not_retrieved_from_queue() { const int N = 200; const int N_THREADS = 3; var sut = new Parallelize(N_THREADS); var are = new AutoResetEvent(false); var results = new List <int>(); var threads = new Dictionary <int, bool>(); Action <IMessage> dequeue = _ => { lock (results) { if (!threads.ContainsKey(Thread.CurrentThread.ManagedThreadId)) { threads.Add(Thread.CurrentThread.ManagedThreadId, true); } var i = (int)_.Data; results.Add(i); if (results.Count == N) { are.Set(); } Thread.Sleep(i % 20); } }; sut.Start(); for (var i = 1; i <= N; i++) { sut.Process(new Message("x", i), dequeue); } Assert.IsTrue(are.WaitOne(4000)); Assert.AreEqual((N * (N + 1) / 2), results.Sum()); Assert.AreEqual(N_THREADS, threads.Count); }
public IndexFiles() { // Build var crawlDirTree = new Crawl_directory_tree(); var compileFiles = new Compile_files(crawlDirTree); var readLines = new Read_text_lines(); var splitLinesIntoWords = new Split_lines_into_words(); var readWords = new Read_words(readLines, splitLinesIntoWords); var filterWords = new Filter_words(); var extractWords = new Extract_words(readWords, filterWords); var compileWords = new Compile_words(); var writeIndexToFile = new Write_index_to_file(); var buildIndex = new Build_index(compileWords, writeIndexToFile); var logValidationError = new Log<string>(errMsg => "*** " + errMsg); var logException = new Log<Exception>(ex => "*** " + ex.Message + '\n' + ex.StackTrace); var handleEx = new HandleException<Tuple<string, string>>(); var asyncCompileFiles = new Asynchronize<Tuple<string, string>>(); var asyncBuildIndex = new Asynchronize<Tuple<string, string[]>>(); var countFilesFound = new CountItemsUntilNullForScatter<string>(); var parExtractWords = new Parallelize<string>(); var finishStreamOfFileWordsWithNull = new InsertNullAfterItemsForGather<Tuple<string, string[]>>(); // Bind this.in_Process = _ => asyncCompileFiles.In_Process(_); asyncCompileFiles.Out_ProcessSequentially += handleEx.In_Process; handleEx.Out_Process += compileFiles.In_Process; handleEx.Out_Exception += logException.In_Process; logException.Out_Data += _ => this.Out_UnhandledException(_); compileFiles.Out_FileFound += countFilesFound.In_Count; compileFiles.Out_FileFound += filename => { if (filename != null) this.Out_FileFoundToIndex(filename); }; countFilesFound.Out_Counted += parExtractWords.In_Process; parExtractWords.Out_ProcessInParallel += extractWords.In_Process; countFilesFound.Out_Count += finishStreamOfFileWordsWithNull.In_NumberOfItemsToGather; extractWords.Out_WordsExtracted += finishStreamOfFileWordsWithNull.In_Process; finishStreamOfFileWordsWithNull.Out_Gather += asyncBuildIndex.In_Process; asyncBuildIndex.Out_ProcessSequentially += buildIndex.In_Process; compileFiles.Out_IndexFilename += buildIndex.In_IndexFilename; compileFiles.Out_ValidationError += logValidationError.In_Process; logValidationError.Out_Data += _ => this.Out_ValidationError(_); buildIndex.Out_Statistics += _ => this.Out_Statistics(_); }