Back to index

lightning-sunbird  0.9+nobinonly
NotifyAll.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 //NotifyAll
00039 TM_OJIAPITest(ThreadManager_NotifyAll_1) {
00040        GET_TM_FOR_TEST
00041        nsresult rc = threadMgr->NotifyAll(NULL);
00042        if (NS_SUCCEEDED(rc))
00043               return TestResult::PASS("Method should fail because adress is invalid (NULL)");
00044        return TestResult::FAIL("NotifyAll", rc);
00045 }
00046 
00047 TM_OJIAPITest(ThreadManager_NotifyAll_2) {
00048        GET_TM_FOR_TEST
00049        nsresult rc = threadMgr->NotifyAll(threadMgr);
00050        if (NS_SUCCEEDED(rc))
00051               return TestResult::PASS("Method should fail because it can call Notify only being monitor's owner.");
00052        return TestResult::FAIL("NotifyAll", rc);
00053 
00054 }
00055 
00056 
00057 TM_OJIAPITest(ThreadManager_NotifyAll_3) {
00058        GET_TM_FOR_TEST
00059        class DummyThread : public BaseDummyThread {
00060        public:
00061               int notified;
00062               DummyThread *otherThread;
00063               DummyThread(nsIThreadManager *threadMgr, nsresult def_rc) : notified(0) {
00064                      tm = threadMgr;
00065                      rc = def_rc; 
00066               }
00067               NS_METHOD Run() { 
00068                      nsresult lrc = tm->EnterMonitor(tm);
00069                      printf("Thread %p waiting for monitor %p\n", this, tm);
00070                      lrc = tm->Wait(tm);
00071                      if (NS_SUCCEEDED(lrc))  {
00072                             printf("Thread %p: after wait -> notified: %d\n", this, notified);
00073                             //if first thread notified it - nsresult rc =ing OK
00074                             if (notified == 1) {  
00075                                    this->rc = NS_OK;
00076                             }
00077                             tm->ExitMonitor(tm);
00078                             //other thread should get here without any notifications
00079                      } else {
00080                             printf("ERROR: thread %p: Wait method returned error (%d)\n", this, lrc);
00081                      }
00082                      while(1);
00083                      return NS_OK;
00084               }
00085        };
00086 
00087        PRUint32 id = 0;
00088        DummyThread *newThread1 = new DummyThread(threadMgr, NS_ERROR_FAILURE);
00089        DummyThread *newThread2 = new DummyThread(threadMgr, NS_ERROR_FAILURE);
00090        nsresult rc1  = threadMgr->CreateThread(&id, (nsIRunnable*)newThread1);
00091        nsresult rc2  = threadMgr->CreateThread(&id, (nsIRunnable*)newThread2);
00092 
00093        newThread1->otherThread = newThread2;
00094        newThread2->otherThread = newThread1;
00095        //time to enter monitor and say wait
00096        threadMgr->Sleep((PRUint32)500);
00097        nsresult rc = threadMgr->EnterMonitor(threadMgr);
00098        if (NS_SUCCEEDED(rc)) {
00099               if (NS_SUCCEEDED(rc)) {
00100                      newThread1->notified = 1;
00101                      newThread2->notified = 1;          
00102                      rc = threadMgr->NotifyAll(threadMgr);
00103                      threadMgr->ExitMonitor(threadMgr);
00104                      if (NS_SUCCEEDED(rc)) {
00105                             //time to check notified flag and set rc to NS_OK
00106                             threadMgr->Sleep((PRUint32)500);
00107                             if (NS_SUCCEEDED(newThread1->rc) && NS_SUCCEEDED(newThread2->rc))
00108                                    return TestResult::PASS("Method should work OK.");
00109                             return TestResult::FAIL("Not all threads were notified.");
00110                      } else {
00111                             //can't call Notify method
00112                             return TestResult::FAIL("NotifyAll", rc);
00113                      }
00114               } else {
00115                      return TestResult::FAIL("NotifyAll", "Main thread can't exit monitor", rc);
00116               }
00117        } else {
00118               //can't create new threds
00119               return TestResult::FAIL("NotifyAll", "Can't create new threds", rc);
00120        }
00121        //can't enter monitor
00122        return TestResult::FAIL("NotifyAll", "Can't enter monitor", rc);
00123        
00124 }
00125 
00126 TM_OJIAPITest(ThreadManager_NotifyAll_4) {
00127        GET_TM_FOR_TEST
00128        nsresult rc = threadMgr->EnterMonitor(threadMgr);
00129        
00130        if (NS_SUCCEEDED(rc)) {
00131               nsresult rc = threadMgr->NotifyAll(threadMgr);
00132               if (NS_SUCCEEDED(rc))
00133                      return TestResult::PASS("Method should work even if there are no other threads waiting on monitor.");
00134               return TestResult::FAIL("NotifyAll", rc);
00135        }
00136        return TestResult::FAIL("NotifyAll", "Can't enter monitor", rc);
00137 
00138 }
00139 
00140 
00141