Exemplo n.º 1
0
        public void ThreadLock_SimpleTests()
        {
            UT_INIT();
            Report.GetDefault().PushHaltFlags(false, false);

            Log.AddDebugLogger();
            Log.MapThreadName("UnitTest");
            Log.SetDomain("TestTLock", Scope.Method);
            Log.SetVerbosity(Log.DebugLogger, Verbosity.Verbose, "ALIB");

            // lock a recursive lock
            ThreadLock aLock = new ThreadLock();

            aLock.Acquire();
            aLock.Release();

            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));

            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                                UT_TRUE(aLock.ToString().StartsWith("Unlocked"));

            // set unsafe
            aLock.SetSafeness(Safeness.Unsafe);           UT_TRUE(aLock.ToString().StartsWith("Unlocked"));
            UT_TRUE(aLock.ToString().Contains("Unsafe"));
            aLock.SetSafeness(Safeness.Safe);             UT_TRUE(!aLock.ToString().Contains("Unsafe"));

            aLock.SetSafeness(Safeness.Unsafe);           UT_TRUE(aLock.ToString().StartsWith("Unlocked"));
            UT_TRUE(aLock.ToString().Contains("Unsafe"));
            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                                UT_TRUE(aLock.ToString().StartsWith("Unlocked"));
            UT_TRUE(aLock.ToString().Contains("Unsafe"));

            // unsafe
            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            Log.Info("One warning should come now: ");
            aLock.SetSafeness(Safeness.Safe);             UT_TRUE(aLock.ToString().StartsWith("Locked"));
            UT_TRUE(aLock.ToString().Contains("Unsafe"));

            // safe (new lock)
            aLock = new ThreadLock();
            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            UT_TRUE(!aLock.ToString().Contains("Unsafe"));
            Log.Info("One warning should come now: ");
            aLock.SetSafeness(Safeness.Unsafe);           UT_TRUE(!aLock.ToString().StartsWith("null"));
            UT_TRUE(!aLock.ToString().Contains("Unsafe"));


            // test warnings (10) locks:
            aLock = new ThreadLock();
            Log.Info("Two warnings should come now: ");
            for (int i = 0; i < 20; i++)
            {
                aLock.Acquire();
            }
            UT_TRUE(aLock.ToString().StartsWith("Locked"));
            for (int i = 0; i < 20; i++)
            {
                aLock.Release();
            }
            UT_TRUE(aLock.ToString().StartsWith("Unlocked"));

            // test a non-recursive lock
            aLock = new ThreadLock(LockMode.SingleLocks);
            aLock.Acquire();                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Acquire();                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                UT_TRUE(aLock.ToString().StartsWith("Unlocked"));

            Log.Info("One warning should come now: ");
            aLock.Release();                UT_TRUE(aLock.ToString().StartsWith("Unlocked"));

            Report.GetDefault().PopHaltFlags();
        }