thread.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. -- love.thread
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ----------------------------------OBJECTS---------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- Channel (love.thread.newChannel)
  8. love.test.thread.Channel = function(test)
  9. -- create channel
  10. local channel = love.thread.getChannel('test')
  11. test:assertObject(channel)
  12. -- setup thread to use
  13. local threadcode1 = [[
  14. require("love.timer")
  15. love.timer.sleep(0.1)
  16. love.thread.getChannel('test'):push('hello world')
  17. love.timer.sleep(0.1)
  18. love.thread.getChannel('test'):push('me again')
  19. ]]
  20. local thread1 = love.thread.newThread(threadcode1)
  21. thread1:start()
  22. -- check message sent from thread to channel
  23. local msg1 = channel:demand()
  24. test:assertEquals('hello world', msg1, 'check 1st message was sent')
  25. thread1:wait()
  26. test:assertEquals(1, channel:getCount(), 'check still another message')
  27. test:assertEquals('me again', channel:peek(), 'check 2nd message pending')
  28. local msg2 = channel:pop()
  29. test:assertEquals('me again', msg2, 'check 2nd message was sent')
  30. channel:clear()
  31. -- setup another thread for some ping pong
  32. local threadcode2 = [[
  33. local function setChannel(channel, value)
  34. channel:clear()
  35. return channel:push(value)
  36. end
  37. local channel = love.thread.getChannel('test')
  38. local waiting = true
  39. local sent = nil
  40. while waiting == true do
  41. if sent == nil then
  42. sent = channel:performAtomic(setChannel, 'ping')
  43. end
  44. if channel:hasRead(sent) then
  45. local msg = channel:demand()
  46. if msg == 'pong' then
  47. channel:push(msg)
  48. waiting = false
  49. end
  50. end
  51. end
  52. ]]
  53. -- first we run a thread that will send 1 ping
  54. local thread2 = love.thread.newThread(threadcode2)
  55. thread2:start()
  56. -- we wait for that ping to be sent and then send a pong back
  57. local msg3 = channel:demand()
  58. test:assertEquals('ping', msg3, 'check message recieved 1')
  59. -- thread should be waiting for us, and checking is the ping was read
  60. channel:supply('pong', 1)
  61. -- if it was then it should send back our pong and thread should die
  62. thread2:wait()
  63. local msg4 = channel:pop()
  64. test:assertEquals('pong', msg4, 'check message recieved 2')
  65. test:assertEquals(0, channel:getCount())
  66. end
  67. -- Thread (love.thread.newThread)
  68. love.test.thread.Thread = function(test)
  69. -- create thread
  70. local threadcode = [[
  71. local b = 0
  72. for a=1,100000 do
  73. b = b + a
  74. end
  75. ]]
  76. local thread = love.thread.newThread(threadcode)
  77. test:assertObject(thread)
  78. -- check thread runs
  79. thread:start()
  80. test:assertTrue(thread:isRunning(), 'check started')
  81. thread:wait()
  82. test:assertFalse(thread:isRunning(), 'check finished')
  83. test:assertEquals(nil, thread:getError(), 'check no errors')
  84. -- check an invalid thread
  85. local badthreadcode = 'local b = 0\nreturn b + "string" .. 10'
  86. local badthread = love.thread.newThread(badthreadcode)
  87. badthread:start()
  88. badthread:wait()
  89. test:assertNotNil(badthread:getError())
  90. end
  91. --------------------------------------------------------------------------------
  92. --------------------------------------------------------------------------------
  93. ----------------------------------METHODS---------------------------------------
  94. --------------------------------------------------------------------------------
  95. --------------------------------------------------------------------------------
  96. -- love.thread.getChannel
  97. -- @NOTE this is just basic nil checking, objs have their own test method
  98. love.test.thread.getChannel = function(test)
  99. test:assertObject(love.thread.getChannel('test'))
  100. end
  101. -- love.thread.newChannel
  102. -- @NOTE this is just basic nil checking, objs have their own test method
  103. love.test.thread.newChannel = function(test)
  104. test:assertObject(love.thread.newChannel())
  105. end
  106. -- love.thread.newThread
  107. -- @NOTE this is just basic nil checking, objs have their own test method
  108. love.test.thread.newThread = function(test)
  109. test:assertObject(love.thread.newThread('classes/TestSuite.lua'))
  110. end