netwerk/test/TestIOThreads.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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)
    15 //
    16 // set NSPR_LOG_MODULES=Test:5
    17 //
    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;
    31     }
    33 private:
    34     int mIndex;
    35 };
    36 NS_IMPL_ISUPPORTS(nsIOEvent, nsIRunnable)
    38 static nsresult RunTest()
    39 {
    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);
    50     }
    52     return NS_OK;
    53 }
    55 int main(int argc, char **argv)
    56 {
    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;
    79 }

mercurial