PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/drillbit/Resources/tests/Network_TCPSocket/Network_TCPSocket.js

http://github.com/appcelerator/titanium_desktop
JavaScript | 123 lines | 92 code | 17 blank | 14 comment | 0 complexity | 385f09527d608880e750a271542d78f9 MD5 | raw file
Possible License(s): Apache-2.0
  1. describe("Network.TCPSocket",{
  2. // ti.network.HTTP objects are covered in the http_server unit test.
  3. before_all: function()
  4. {
  5. // Launch testing server on port 8080
  6. /*this.testServer = Titanium.Process.createProcess(
  7. {
  8. args: [
  9. 'python', Titanium.API.application.resourcesPath + "/testserver.py"
  10. ],
  11. });
  12. this.testServer.launch();*/
  13. },
  14. after_all: function()
  15. {
  16. //this.testServer.kill();
  17. },
  18. before: function()
  19. {
  20. // Create a test socket client
  21. this.socket = Titanium.Network.createTCPSocket("127.0.0.1", 8080);
  22. },
  23. // test the network object and properties.
  24. test_TCPSocket_object: function()
  25. {
  26. value_of(this.socket).should_be_object();
  27. value_of(this.socket.close).should_be_function();
  28. value_of(this.socket.connect).should_be_function();
  29. value_of(this.socket.isClosed).should_be_function();
  30. value_of(this.socket.write).should_be_function();
  31. },
  32. test_connect_as_async: function(test)
  33. {
  34. var timer;
  35. var socket = this.socket;
  36. socket.on("connect", function()
  37. {
  38. clearTimeout(timer);
  39. socket.close();
  40. test.passed();
  41. });
  42. socket.on("error", function(err)
  43. {
  44. clearTimeout(timer);
  45. test.failed(err);
  46. });
  47. socket.connect();
  48. timer = setTimeout(function()
  49. {
  50. test.failed("Test timed out");
  51. }, 2000);
  52. },
  53. test_timeout_as_async: function(test)
  54. {
  55. var timer;
  56. var socket = this.socket;
  57. socket.setTimeout(1000);
  58. socket.on("timeout", function()
  59. {
  60. clearTimeout(timer);
  61. socket.close();
  62. test.passed();
  63. });
  64. socket.connect();
  65. timer = setTimeout(function()
  66. {
  67. test.failed("Test timed out");
  68. }, 2000);
  69. },
  70. test_read_write_as_async: function(test)
  71. {
  72. var timer;
  73. var message = "hello, can anyone hear me?";
  74. var socket = this.socket;
  75. socket.on("connect", function()
  76. {
  77. // Send test server a message.
  78. socket.write(message);
  79. });
  80. socket.on("data", function(data)
  81. {
  82. clearTimeout(timer);
  83. try
  84. {
  85. // Test server should echo the message back.
  86. value_of(data).should_be(message);
  87. socket.close();
  88. test.passed();
  89. }
  90. catch (e)
  91. {
  92. test.failed(e);
  93. }
  94. });
  95. socket.on("error", function(err)
  96. {
  97. clearTimeout(timer);
  98. test.failed(err);
  99. });
  100. socket.connect();
  101. timer = setTimeout(function()
  102. {
  103. test.failed("Test timed out");
  104. }, 2000);
  105. }
  106. });