Guest User

Untitled

a guest
Feb 21st, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. require File.join(File.dirname(__FILE__), 'CONFIG.rb')
  2.  
  3. require 'rubygems'
  4. require 'facets'
  5. require 'test/unit'
  6. require 'og'
  7.  
  8. class TCOgManager < Test::Unit::TestCase # :nodoc: all
  9. include Og
  10.  
  11. class User
  12. attr_accessor :name, String
  13. end
  14.  
  15. $og1.manage_classes(User)
  16.  
  17.  
  18. def setup
  19. 10.times {User.create}
  20. end
  21.  
  22.  
  23. # Query Og via 'infinite' threads.
  24. # This is heavily bias towards SQLite and probably won't fail with other
  25. # adapters.
  26. # If the connection pool is not managed correctly then you may:
  27. # 1. You will leak file descriptors/memory.
  28. # 2. Block due to incorrect semaphore coordination.
  29. #
  30. # I assume you're running with a standard 1024 maximum Linux file descriptor.
  31.  
  32. def test_connection_pool
  33. #number of threads to run concurrently.
  34. max_threads = 1500
  35. #maximum length test should run for.
  36. timeout = 60
  37. #poll threads every n seconds
  38. poll = 5
  39.  
  40. Thread.abort_on_exception = true
  41. completed = 0
  42. max_threads.times do
  43. Thread.new(completed) do |c|
  44. User[1]
  45. completed +=1
  46. end
  47. end
  48.  
  49. #Sleep until exception occurs or 1500 threads have completed.
  50. #If they completed then the test passes!
  51. slept = 0
  52. until completed == max_threads or slept >= timeout
  53. sleep(poll)
  54. slept += poll
  55. end
  56.  
  57. assert slept < timeout
  58.  
  59. #Probably a good time to clean up.
  60. GC.start
  61. end
  62.  
  63.  
  64. end
Add Comment
Please, Sign In to add comment