Bumping manifests a=b2g-bump
[gecko.git] / netwerk / test / TestIOThreads.cpp
blobf2ca8aeb6934a9e2157baa7c88b87df98b842663
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "TestCommon.h"
6 #include "nsXPCOM.h"
7 #include "nsIServiceManager.h"
8 #include "nsServiceManagerUtils.h"
9 #include "nsIEventTarget.h"
10 #include "nsCOMPtr.h"
11 #include "nsNetCID.h"
12 #include "prlog.h"
14 #if defined(PR_LOGGING)
16 // set NSPR_LOG_MODULES=Test:5
18 static PRLogModuleInfo *gTestLog = nullptr;
19 #endif
20 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
22 class nsIOEvent : public nsIRunnable {
23 public:
24 NS_DECL_THREADSAFE_ISUPPORTS
26 nsIOEvent(int i) : mIndex(i) {}
28 NS_IMETHOD Run() {
29 LOG(("Run [%d]\n", mIndex));
30 return NS_OK;
33 private:
34 int mIndex;
36 NS_IMPL_ISUPPORTS(nsIOEvent, nsIRunnable)
38 static nsresult RunTest()
40 nsresult rv;
41 nsCOMPtr<nsIEventTarget> target =
42 do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
43 if (NS_FAILED(rv))
44 return rv;
46 for (int i=0; i<10; ++i) {
47 nsCOMPtr<nsIRunnable> event = new nsIOEvent(i);
48 LOG(("Dispatch %d\n", i));
49 target->Dispatch(event, NS_DISPATCH_NORMAL);
52 return NS_OK;
55 int main(int argc, char **argv)
57 if (test_common_init(&argc, &argv) != 0)
58 return -1;
60 nsresult rv;
62 #if defined(PR_LOGGING)
63 gTestLog = PR_NewLogModule("Test");
64 #endif
66 rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
67 if (NS_FAILED(rv))
68 return rv;
70 rv = RunTest();
71 if (NS_FAILED(rv))
72 LOG(("RunTest failed [rv=%x]\n", rv));
74 LOG(("sleeping main thread for 2 seconds...\n"));
75 PR_Sleep(PR_SecondsToInterval(2));
77 NS_ShutdownXPCOM(nullptr);
78 return 0;