Back to index

lightning-sunbird  0.9+nobinonly
EnterMonitor.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <ThreadManagerTests.h>
00038 /*EnterMonitor*/
00039 
00040 TM_OJIAPITest(ThreadManager_EnterMonitor_1) {
00041        GET_TM_FOR_TEST
00042        nsresult rc = threadMgr->EnterMonitor(NULL);
00043        if (NS_FAILED(rc))
00044               return TestResult::PASS("Method shoulf fail because invalid (NULL) adress is specified.");
00045        return TestResult::FAIL("EnterMonitor", rc);
00046 
00047 }
00048 
00049 TM_OJIAPITest(ThreadManager_EnterMonitor_2) {
00050        GET_TM_FOR_TEST
00051        //printf("Entering monitor with adress %p\n", threadMgr);
00052        nsresult rc = threadMgr->EnterMonitor(threadMgr);
00053        if (NS_SUCCEEDED(rc))
00054               return TestResult::PASS("Current thread can enter monitor even if no other thread we created.");
00055        return TestResult::FAIL("EnterMonitor", rc);
00056 
00057 }
00058 
00059 TM_OJIAPITest(ThreadManager_EnterMonitor_3) {
00060        GET_TM_FOR_TEST
00061        nsresult rc = threadMgr->EnterMonitor(threadMgr);
00062        printf("Entering monitor with adress %p\n", threadMgr);
00063        if (NS_SUCCEEDED(rc)) {
00064               printf("Entering monitor again with adress %p\n", threadMgr);
00065               nsresult rc = threadMgr->EnterMonitor(threadMgr);
00066               if (NS_SUCCEEDED(rc))
00067                      return TestResult::PASS("One thread can enter monitor twice.");
00068               return TestResult::FAIL("EnterMonitor", rc);
00069        }
00070        return TestResult::FAIL("EnterMonitor", "Can't enter monitor at all", rc);
00071 
00072 }
00073 
00074 
00075 TM_OJIAPITest(ThreadManager_EnterMonitor_4) {
00076        GET_TM_FOR_TEST
00077        class DummyThread : public BaseDummyThread {
00078        public:
00079               DummyThread(nsIThreadManager *threadMgr, nsresult def_rc) {
00080                      tm = threadMgr;
00081                      rc = def_rc; 
00082               }
00083               NS_METHOD Run() { 
00084                      //printf("New thread enters monitor with adress %p ... ", tm); 
00085                      tm->EnterMonitor(tm); 
00086                      rc = NS_ERROR_FAILURE;
00087                      //printf("Error - new thread should wait while first thread exit monitor !\n"); 
00088                      while(1); 
00089                      return NS_OK;
00090               }
00091        };
00092        PRUint32 id = 0;
00093        DummyThread *newThread = new DummyThread(threadMgr, NS_OK);
00094 
00095        printf("Our thread enters monitor with adress %p (%d) ...\n", threadMgr, id);
00096        nsresult rc = threadMgr->EnterMonitor(threadMgr);
00097        if (NS_SUCCEEDED(rc)) {
00098               rc = threadMgr->CreateThread(&id, (nsIRunnable*)newThread);
00099               if (NS_SUCCEEDED(rc))  {
00100                      //let's give new thread a second to call EnterMonitor method
00101                      threadMgr->Sleep(1000);
00102                      rc = newThread->rc;
00103                      if (NS_SUCCEEDED(rc))
00104                             return TestResult::PASS("Another thread can't enter monitor locked by current thread.");
00105                      return TestResult::FAIL("EnterMonitor", rc);
00106 
00107               }
00108               return TestResult::FAIL("EnterMonitor", "Can't create new thread", rc);
00109 
00110        }
00111        printf("First thread can't enter monitor ...\n");
00112        return TestResult::FAIL("EnterMonitor", "First thread can't enter monitor", rc);
00113 
00114 }
00115