示例#1
0
        public void TestMultiThreaded()
        {
            FileInfo    file    = new FileInfo(Path.Combine(getWorkDir().FullName, "one-line"));
            PerfRunData runData = createPerfRunData(file, false, typeof(ThreadingDocMaker).AssemblyQualifiedName);

            ThreadJob[] threads = new ThreadJob[10];
            using (WriteLineDocTask wldt = new WriteLineDocTask(runData))
            {
                for (int i = 0; i < threads.Length; i++)
                {
                    threads[i] = new ThreadAnonymousHelper("t" + i, wldt);
                }

                foreach (ThreadJob t in threads)
                {
                    t.Start();
                }
                foreach (ThreadJob t in threads)
                {
                    t.Join();
                }
            } // wldt.Dispose();

            ISet <String> ids = new JCG.HashSet <string>();
            TextReader    br  = new StreamReader(new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.None), Encoding.UTF8);

            try
            {
                String line = br.ReadLine();
                assertHeaderLine(line); // header line is written once, no matter how many threads there are
                for (int i = 0; i < threads.Length; i++)
                {
                    line = br.ReadLine();
                    assertNotNull($"line for index {i} is missing", line); // LUCENENET specific - ensure the line is there before splitting
                    String[] parts = line.Split(WriteLineDocTask.SEP).TrimEnd();
                    assertEquals(line, 3, parts.Length);
                    // check that all thread names written are the same in the same line
                    String tname = parts[0].Substring(parts[0].IndexOf('_'));
                    ids.add(tname);
                    assertEquals(tname, parts[1].Substring(parts[1].IndexOf('_')));
                    assertEquals(tname, parts[2].Substring(parts[2].IndexOf('_')));
                }
                // only threads.length lines should exist
                assertNull(br.ReadLine());
                assertEquals(threads.Length, ids.size());
            }
            finally
            {
                br.Dispose();
            }
        }
示例#2
0
        public void TestICUConcurrency()
        {
            int            numThreads  = 8;
            CountdownEvent startingGun = new CountdownEvent(1);

            ThreadAnonymousHelper[] threads = new ThreadAnonymousHelper[numThreads];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new ThreadAnonymousHelper(startingGun);

                threads[i].Start();
            }
            startingGun.Signal();
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }
        }
        public void TestMultiThreaded()
        {
            FileInfo         file    = new FileInfo(Path.Combine(getWorkDir().FullName, "one-line"));
            PerfRunData      runData = createPerfRunData(file, false, typeof(ThreadingDocMaker).AssemblyQualifiedName);
            WriteLineDocTask wldt    = new WriteLineDocTask(runData);

            ThreadClass[] threads = new ThreadClass[10];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new ThreadAnonymousHelper("t" + i, wldt);
            }

            foreach (ThreadClass t in threads)
            {
                t.Start();
            }
            foreach (ThreadClass t in threads)
            {
                t.Join();
            }

            wldt.Dispose();

            // LUCENENET specific - need to transfer any exception that occurred back to this thread
            foreach (ThreadClass t in threads)
            {
                var thread = t as ThreadAnonymousHelper;

                if (thread?.Exception != null)
                {
                    throw thread.Exception;
                }
            }

            ISet <String> ids = new HashSet <string>();
            TextReader    br  = new StreamReader(new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);

            try
            {
                String line = br.ReadLine();
                assertHeaderLine(line); // header line is written once, no matter how many threads there are
                for (int i = 0; i < threads.Length; i++)
                {
                    line = br.ReadLine();
                    String[] parts = line.Split(WriteLineDocTask.SEP).TrimEnd();
                    assertEquals(line, 3, parts.Length);
                    // check that all thread names written are the same in the same line
                    String tname = parts[0].Substring(parts[0].IndexOf('_'));
                    ids.add(tname);
                    assertEquals(tname, parts[1].Substring(parts[1].IndexOf('_')));
                    assertEquals(tname, parts[2].Substring(parts[2].IndexOf('_')));
                }
                // only threads.length lines should exist
                assertNull(br.ReadLine());
                assertEquals(threads.Length, ids.size());
            }
            finally
            {
                br.Dispose();
            }
        }