Пример #1
0
        public void SerializedWorkerRaceTest()
        {
            int callCount = 0;

            Func <object, bool> func = (o) =>
            {
                if (callCount == 0)
                {
                    // call continue but return not completed
                    // to see if the worker can handle this race
                    SerializedWorker <object> worker = (SerializedWorker <object>)o;
                    worker.ContinueWork();
                    ++callCount;
                    return(false);
                }
                else
                {
                    callCount = 100;
                    return(true);
                }
            };

            SerializedWorker <object> serialziedWorker = new SerializedWorker <object>(new Worker <object>(func));

            serialziedWorker.DoWork(serialziedWorker);
            Assert.True(callCount == 100, "the work is not done even if continue is called.");
        }
Пример #2
0
        public void SerializedWorkerTest()
        {
            const int        workerCount    = 3;
            const int        workLoad       = 1000000;
            const int        totalCount     = workLoad * workerCount;
            int              completedCount = 0;
            int              callCount      = 0;
            ManualResetEvent completeEvent  = new ManualResetEvent(false);

            WaitCallback callContinue = null;

            Func <object, bool> func = (o) =>
            {
                bool workCompleted = callCount++ % 30000 != 0;
                if (workCompleted)
                {
                    if (++completedCount == totalCount)
                    {
                        completeEvent.Set();
                    }
                }
                else
                {
                    ThreadPool.QueueUserWorkItem(callContinue, null);
                }

                return(workCompleted);
            };

            ParameterizedThreadStart producer = (o) =>
            {
                SerializedWorker <object> worker = (SerializedWorker <object>)o;
                for (int i = 0; i < workLoad; ++i)
                {
                    worker.DoWork(o);
                }
            };

            SerializedWorker <object> serialziedWorker = new SerializedWorker <object>(new Worker <object>(func));

            callContinue = (o) => { serialziedWorker.ContinueWork(); };

            Thread[] workerThreads = new Thread[workerCount];
            for (int i = 0; i < workerThreads.Length; ++i)
            {
                workerThreads[i] = new Thread(producer);
                workerThreads[i].Start(serialziedWorker);
            }

            // issue some continue work signal
            for (int i = 0; i < 20000; ++i)
            {
                serialziedWorker.ContinueWork();
            }

            bool waitOne = completeEvent.WaitOne(60 * 1000);

            Debug.WriteLine(string.Format("total: {0}, completed: {1}", totalCount, completedCount));
            if (!waitOne)
            {
                Assert.True(false, "Worker did not complete in time");
            }
            else
            {
                Assert.True(totalCount == completedCount, "Completed count is not correct.");
            }
        }
Пример #3
0
        public void SerializedWorkerTest()
        {
            const int        workerCount    = 3;
            const int        workLoad       = 1000000;
            const int        totalCount     = workLoad * workerCount;
            int              completedCount = 0;
            int              callCount      = 0;
            int              working        = 0;
            ManualResetEvent completeEvent  = new ManualResetEvent(false);

            WaitCallback callContinue = null;

            Func <object, bool> func = (o) =>
            {
                // this function should be always called single threaded
                if (Interlocked.Exchange(ref working, 1) == 1)
                {
                    Debug.WriteLine("Should not be called while working");
                    completeEvent.Set();
                    return(false);
                }

                bool workCompleted = callCount++ % 30000 != 0;
                if (workCompleted)
                {
                    if (++completedCount == totalCount)
                    {
                        completeEvent.Set();
                    }
                }

                if (!workCompleted)
                {
                    ThreadPool.QueueUserWorkItem(callContinue, null);
                }

                Interlocked.Exchange(ref working, 0);
                return(workCompleted);
            };

            ParameterizedThreadStart producer = (o) =>
            {
                SerializedWorker <object> worker = (SerializedWorker <object>)o;
                for (int i = 0; i < workLoad; ++i)
                {
                    worker.DoWork(o);
                }
            };

            SerializedWorker <object> serialziedWorker = new SerializedWorker <object>(new Worker <object>(func));

            callContinue = (o) => { serialziedWorker.ContinueWork(); };

            Thread[] workerThreads = new Thread[workerCount];
            for (int i = 0; i < workerThreads.Length; ++i)
            {
                workerThreads[i] = new Thread(producer);
                workerThreads[i].Start(serialziedWorker);
            }

            // issue some continue work signal
            for (int i = 0; i < 20000; ++i)
            {
                serialziedWorker.ContinueWork();
            }

            // wait for all producer to finish
            for (int i = 0; i < workerThreads.Length; ++i)
            {
                workerThreads[i].Join();
            }

            bool waitOne = completeEvent.WaitOne(30 * 1000);

            Debug.WriteLine(string.Format("total: {0}, completed: {1}", totalCount, completedCount));
            if (!waitOne)
            {
                Assert.True(false, "Worker did not complete in time");
            }
            else
            {
                Assert.True(totalCount == completedCount, "Completed count is not correct.");
            }
        }