1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestIOThreads.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,79 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "TestCommon.h" 1.9 +#include "nsXPCOM.h" 1.10 +#include "nsIServiceManager.h" 1.11 +#include "nsServiceManagerUtils.h" 1.12 +#include "nsIEventTarget.h" 1.13 +#include "nsCOMPtr.h" 1.14 +#include "nsNetCID.h" 1.15 +#include "prlog.h" 1.16 + 1.17 +#if defined(PR_LOGGING) 1.18 +// 1.19 +// set NSPR_LOG_MODULES=Test:5 1.20 +// 1.21 +static PRLogModuleInfo *gTestLog = nullptr; 1.22 +#endif 1.23 +#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) 1.24 + 1.25 +class nsIOEvent : public nsIRunnable { 1.26 +public: 1.27 + NS_DECL_THREADSAFE_ISUPPORTS 1.28 + 1.29 + nsIOEvent(int i) : mIndex(i) {} 1.30 + 1.31 + NS_IMETHOD Run() { 1.32 + LOG(("Run [%d]\n", mIndex)); 1.33 + return NS_OK; 1.34 + } 1.35 + 1.36 +private: 1.37 + int mIndex; 1.38 +}; 1.39 +NS_IMPL_ISUPPORTS(nsIOEvent, nsIRunnable) 1.40 + 1.41 +static nsresult RunTest() 1.42 +{ 1.43 + nsresult rv; 1.44 + nsCOMPtr<nsIEventTarget> target = 1.45 + do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); 1.46 + if (NS_FAILED(rv)) 1.47 + return rv; 1.48 + 1.49 + for (int i=0; i<10; ++i) { 1.50 + nsCOMPtr<nsIRunnable> event = new nsIOEvent(i); 1.51 + LOG(("Dispatch %d\n", i)); 1.52 + target->Dispatch(event, NS_DISPATCH_NORMAL); 1.53 + } 1.54 + 1.55 + return NS_OK; 1.56 +} 1.57 + 1.58 +int main(int argc, char **argv) 1.59 +{ 1.60 + if (test_common_init(&argc, &argv) != 0) 1.61 + return -1; 1.62 + 1.63 + nsresult rv; 1.64 + 1.65 +#if defined(PR_LOGGING) 1.66 + gTestLog = PR_NewLogModule("Test"); 1.67 +#endif 1.68 + 1.69 + rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.70 + if (NS_FAILED(rv)) 1.71 + return rv; 1.72 + 1.73 + rv = RunTest(); 1.74 + if (NS_FAILED(rv)) 1.75 + LOG(("RunTest failed [rv=%x]\n", rv)); 1.76 + 1.77 + LOG(("sleeping main thread for 2 seconds...\n")); 1.78 + PR_Sleep(PR_SecondsToInterval(2)); 1.79 + 1.80 + NS_ShutdownXPCOM(nullptr); 1.81 + return 0; 1.82 +}