TestIOThreads.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/. */
  4. #include "TestCommon.h"
  5. #include "nsXPCOM.h"
  6. #include "nsIServiceManager.h"
  7. #include "nsServiceManagerUtils.h"
  8. #include "nsIEventTarget.h"
  9. #include "nsCOMPtr.h"
  10. #include "nsNetCID.h"
  11. #include "mozilla/Logging.h"
  12. //
  13. // set NSPR_LOG_MODULES=Test:5
  14. //
  15. static PRLogModuleInfo *gTestLog = nullptr;
  16. #define LOG(args) MOZ_LOG(gTestLog, mozilla::LogLevel::Debug, args)
  17. class nsIOEvent : public nsIRunnable {
  18. public:
  19. NS_DECL_THREADSAFE_ISUPPORTS
  20. nsIOEvent(int i) : mIndex(i) {}
  21. NS_IMETHOD Run() override {
  22. LOG(("Run [%d]\n", mIndex));
  23. return NS_OK;
  24. }
  25. private:
  26. int mIndex;
  27. };
  28. NS_IMPL_ISUPPORTS(nsIOEvent, nsIRunnable)
  29. static nsresult RunTest()
  30. {
  31. nsresult rv;
  32. nsCOMPtr<nsIEventTarget> target =
  33. do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
  34. if (NS_FAILED(rv))
  35. return rv;
  36. for (int i=0; i<10; ++i) {
  37. nsCOMPtr<nsIRunnable> event = new nsIOEvent(i);
  38. LOG(("Dispatch %d\n", i));
  39. target->Dispatch(event, NS_DISPATCH_NORMAL);
  40. }
  41. return NS_OK;
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. if (test_common_init(&argc, &argv) != 0)
  46. return -1;
  47. nsresult rv;
  48. gTestLog = PR_NewLogModule("Test");
  49. rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
  50. if (NS_FAILED(rv))
  51. return rv;
  52. rv = RunTest();
  53. if (NS_FAILED(rv))
  54. LOG(("RunTest failed [rv=%x]\n", rv));
  55. LOG(("sleeping main thread for 2 seconds...\n"));
  56. PR_Sleep(PR_SecondsToInterval(2));
  57. NS_ShutdownXPCOM(nullptr);
  58. return 0;
  59. }