Back to index

lightning-sunbird  0.9+nobinonly
Wait.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 
00039 //Wait
00040 
00041 TM_OJIAPITest(ThreadManager_Wait_1) {
00042        GET_TM_FOR_TEST
00043        nsresult rc = threadMgr->Wait(NULL, (PRUint32)100);
00044        if (NS_FAILED(rc))
00045               return TestResult::PASS("Method should fail because invalid adress (NULL) is specified.");
00046        return TestResult::FAIL("Wait", rc);
00047 
00048 }
00049 
00050 TM_OJIAPITest(ThreadManager_Wait_2) {
00051        GET_TM_FOR_TEST
00052        class DummyThread : public BaseDummyThread {
00053        public:
00054               DummyThread(nsIThreadManager *aTM) { rc = NS_OK; tm = aTM; }
00055               NS_METHOD Run() {
00056                      tm->EnterMonitor(tm);
00057                      tm->Wait(tm, (PRUint32)UINT_MAX);
00058                      rc = NS_ERROR_FAILURE; //we shoudn't ever get here
00059                      tm->ExitMonitor(tm);
00060                      return NS_OK; 
00061               }
00062        };
00063        PRUint32 id = 0;
00064        nsresult rc;
00065 
00066        DummyThread *dt = new DummyThread(threadMgr);
00067        rc = threadMgr->CreateThread(&id, (nsIRunnable*)dt);
00068        threadMgr->Sleep(500); 
00069        rc = threadMgr->EnterMonitor(threadMgr);
00070        if (NS_FAILED(rc))
00071               return TestResult::FAIL("Wait","Can't eneter monitor", rc);
00072        threadMgr->Sleep(500);
00073        if (NS_SUCCEEDED(dt->rc))
00074               return TestResult::PASS("Method should work OK.");
00075        rc = threadMgr->ExitMonitor(threadMgr);
00076        if (NS_FAILED(rc))
00077               return TestResult::FAIL("Wait","Can't exit monitor", rc);
00078        return TestResult::FAIL("Wait", dt->rc);
00079 }
00080 
00081 TM_OJIAPITest(ThreadManager_Wait_3) {
00082        GET_TM_FOR_TEST
00083        class DummyThread : public BaseDummyThread {
00084        public:
00085               DummyThread(nsIThreadManager *aTM) { rc = NS_OK; tm = aTM; }
00086               NS_METHOD Run() {
00087                      tm->EnterMonitor(tm);
00088                      tm->Wait(tm, (PRUint32)0);
00089                      rc = NS_ERROR_FAILURE; //we shoudn't ever get here
00090                      tm->ExitMonitor(tm);
00091                      return NS_OK; 
00092               }
00093        };
00094        PRUint32 id = 0;
00095        nsresult rc;
00096 
00097        DummyThread *dt = new DummyThread(threadMgr);
00098        rc = threadMgr->CreateThread(&id, (nsIRunnable*)dt);
00099        threadMgr->Sleep(500); 
00100        rc = threadMgr->EnterMonitor(threadMgr);
00101        if (NS_FAILED(rc))
00102               return TestResult::FAIL("Wait","Can't eneter monitor", rc);
00103        threadMgr->Sleep(500);
00104        if (NS_SUCCEEDED(dt->rc))
00105               return TestResult::PASS("Method should work OK.");
00106        rc = threadMgr->ExitMonitor(threadMgr);
00107        if (NS_FAILED(rc))
00108               return TestResult::FAIL("Wait","Can't exit monitor", rc);
00109        return TestResult::FAIL("Wait", dt->rc);
00110 }
00111 
00112 TM_OJIAPITest(ThreadManager_Wait_4) {
00113        GET_TM_FOR_TEST
00114        nsresult rc = threadMgr->Wait(threadMgr, (PRUint32)100);
00115        if (NS_FAILED(rc))
00116               return TestResult::PASS("Method should fail because current thread doesn't own monitor.");
00117        return TestResult::FAIL("Wait", rc);
00118 
00119 }
00120 
00121 
00122 
00123 TM_OJIAPITest(ThreadManager_Wait_5) {
00124        GET_TM_FOR_TEST
00125        class DummyThread : public BaseDummyThread {
00126        public:
00127               int notified;
00128               DummyThread(nsIThreadManager *threadMgr, nsresult def_rc) : notified(0) {
00129                      tm = threadMgr;
00130                      rc = def_rc; 
00131               }
00132               NS_METHOD Run() { 
00133                      nsresult lrc = tm->EnterMonitor(tm);
00134                      if (NS_SUCCEEDED(lrc))  {
00135                             lrc = tm->Wait(tm);
00136                             //if first thread notified it - nsresult rc =ing OK
00137                             this->rc = NS_ERROR_FAILURE;
00138                      }
00139                      while(1);
00140                      return NS_OK;
00141               }
00142        };
00143        PRUint32 id = 0;
00144        DummyThread *newThread = new DummyThread(threadMgr, NS_OK);
00145 
00146        nsresult rc = threadMgr->EnterMonitor(threadMgr);
00147        if (NS_SUCCEEDED(rc)) {
00148               rc  = threadMgr->CreateThread(&id, (nsIRunnable*)newThread);
00149               if (NS_SUCCEEDED(rc)) {
00150                      threadMgr->Sleep((PRUint32)500);
00151                      if (NS_SUCCEEDED(newThread->rc))
00152                             return TestResult::PASS("Method should work OK.");
00153                      return TestResult::FAIL("Wait", rc);
00154               } else {
00155                      return TestResult::FAIL("Wait", "Can't create new thread", rc);
00156               }
00157        }
00158        return TestResult::FAIL("Wait", "Can't enter moniotor", rc);
00159 }