merge backout. CLOSED TREE
[mozilla-central.git] / netwerk / test / TestIOThreads.cpp
blobc60a154d592e07bfbb3aa8350c4b5bb3717a8ed7
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Mozilla.
16 * The Initial Developer of the Original Code is IBM Corporation.
17 * Portions created by IBM Corporation are Copyright (C) 2003
18 * IBM Corporation. All Rights Reserved.
20 * Contributor(s):
21 * IBM Corp.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "TestCommon.h"
38 #include "nsXPCOM.h"
39 #include "nsIServiceManager.h"
40 #include "nsServiceManagerUtils.h"
41 #include "nsIEventTarget.h"
42 #include "nsCOMPtr.h"
43 #include "nsNetCID.h"
44 #include "prlog.h"
46 #if defined(PR_LOGGING)
48 // set NSPR_LOG_MODULES=Test:5
50 static PRLogModuleInfo *gTestLog = nsnull;
51 #endif
52 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
54 class nsIOEvent : public nsIRunnable {
55 public:
56 NS_DECL_ISUPPORTS
58 nsIOEvent(int i) : mIndex(i) {}
60 NS_IMETHOD Run() {
61 LOG(("Run [%d]\n", mIndex));
62 return NS_OK;
65 private:
66 int mIndex;
68 NS_IMPL_THREADSAFE_ISUPPORTS1(nsIOEvent, nsIRunnable)
70 static nsresult RunTest()
72 nsresult rv;
73 nsCOMPtr<nsIEventTarget> target =
74 do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
75 if (NS_FAILED(rv))
76 return rv;
78 for (int i=0; i<10; ++i) {
79 nsCOMPtr<nsIRunnable> event = new nsIOEvent(i);
80 LOG(("Dispatch %d\n", i));
81 target->Dispatch(event, NS_DISPATCH_NORMAL);
84 return NS_OK;
87 int main(int argc, char **argv)
89 if (test_common_init(&argc, &argv) != 0)
90 return -1;
92 nsresult rv;
94 #if defined(PR_LOGGING)
95 gTestLog = PR_NewLogModule("Test");
96 #endif
98 rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
99 if (NS_FAILED(rv))
100 return rv;
102 rv = RunTest();
103 if (NS_FAILED(rv))
104 LOG(("RunTest failed [rv=%x]\n", rv));
106 LOG(("sleeping main thread for 2 seconds...\n"));
107 PR_Sleep(PR_SecondsToInterval(2));
109 NS_ShutdownXPCOM(nsnull);
110 return 0;