Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pipeTestServiceClient.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # A Test Program for pipeTestService.py
  2. #
  3. # Install and start the Pipe Test service, then run this test
  4. # either from the same machine, or from another using the "-s" param.
  5. #
  6. # Eg: pipeTestServiceClient.py -s server_name Hi There
  7. # Should work.
  8. import os
  9. import sys
  10. import traceback
  11. import pywintypes
  12. import win32api
  13. import winerror
  14. from win32event import *
  15. from win32file import *
  16. from win32pipe import *
  17. verbose = 0
  18. # def ReadFromPipe(pipeName):
  19. # Could (Should?) use CallNamedPipe, but this technique allows variable size
  20. # messages (whereas you must supply a buffer size for CallNamedPipe!
  21. # hPipe = CreateFile(pipeName, GENERIC_WRITE, 0, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  22. # more = 1
  23. # while more:
  24. # hr = ReadFile(hPipe, 256)
  25. # if hr==0:
  26. # more = 0
  27. # except win32api.error (hr, fn, desc):
  28. # if hr==winerror.ERROR_MORE_DATA:
  29. # data = dat
  30. #
  31. def CallPipe(fn, args):
  32. ret = None
  33. retryCount = 0
  34. while retryCount < 8: # Keep looping until user cancels.
  35. retryCount = retryCount + 1
  36. try:
  37. return fn(*args)
  38. except win32api.error as exc:
  39. if exc.winerror == winerror.ERROR_PIPE_BUSY:
  40. win32api.Sleep(5000)
  41. continue
  42. else:
  43. raise
  44. raise RuntimeError("Could not make a connection to the server")
  45. def testClient(server, msg):
  46. if verbose:
  47. print("Sending", msg)
  48. data = CallPipe(
  49. CallNamedPipe,
  50. ("\\\\%s\\pipe\\PyPipeTest" % server, msg, 256, NMPWAIT_WAIT_FOREVER),
  51. )
  52. if verbose:
  53. print("Server sent back '%s'" % data)
  54. print("Sent and received a message!")
  55. def testLargeMessage(server, size=4096):
  56. if verbose:
  57. print("Sending message of size %d" % (size))
  58. msg = "*" * size
  59. data = CallPipe(
  60. CallNamedPipe,
  61. ("\\\\%s\\pipe\\PyPipeTest" % server, msg, 512, NMPWAIT_WAIT_FOREVER),
  62. )
  63. if len(data) - size:
  64. print("Sizes are all wrong - send %d, got back %d" % (size, len(data)))
  65. def stressThread(server, numMessages, wait):
  66. try:
  67. try:
  68. for i in range(numMessages):
  69. r = CallPipe(
  70. CallNamedPipe,
  71. (
  72. "\\\\%s\\pipe\\PyPipeTest" % server,
  73. "#" * 512,
  74. 1024,
  75. NMPWAIT_WAIT_FOREVER,
  76. ),
  77. )
  78. except:
  79. traceback.print_exc()
  80. print("Failed after %d messages" % i)
  81. finally:
  82. SetEvent(wait)
  83. def stressTestClient(server, numThreads, numMessages):
  84. import _thread
  85. thread_waits = []
  86. for t_num in range(numThreads):
  87. # Note I could just wait on thread handles (after calling DuplicateHandle)
  88. # See the service itself for an example of waiting for the clients...
  89. wait = CreateEvent(None, 0, 0, None)
  90. thread_waits.append(wait)
  91. _thread.start_new_thread(stressThread, (server, numMessages, wait))
  92. # Wait for all threads to finish.
  93. WaitForMultipleObjects(thread_waits, 1, INFINITE)
  94. def main():
  95. import getopt
  96. import sys
  97. server = "."
  98. thread_count = 0
  99. msg_count = 500
  100. try:
  101. opts, args = getopt.getopt(sys.argv[1:], "s:t:m:vl")
  102. for o, a in opts:
  103. if o == "-s":
  104. server = a
  105. if o == "-m":
  106. msg_count = int(a)
  107. if o == "-t":
  108. thread_count = int(a)
  109. if o == "-v":
  110. global verbose
  111. verbose = 1
  112. if o == "-l":
  113. testLargeMessage(server)
  114. msg = " ".join(args).encode("mbcs")
  115. except getopt.error as msg:
  116. print(msg)
  117. my_name = os.path.split(sys.argv[0])[1]
  118. print(
  119. "Usage: %s [-v] [-s server] [-t thread_count=0] [-m msg_count=500] msg ..."
  120. % my_name
  121. )
  122. print(" -v = verbose")
  123. print(
  124. " Specifying a value for -t will stress test using that many threads."
  125. )
  126. return
  127. testClient(server, msg)
  128. if thread_count > 0:
  129. print(
  130. "Spawning %d threads each sending %d messages..."
  131. % (thread_count, msg_count)
  132. )
  133. stressTestClient(server, thread_count, msg_count)
  134. if __name__ == "__main__":
  135. main()