test/node/test_front.rb in zold-0.14.28 vs test/node/test_front.rb in zold-0.14.29

- old
+ new

@@ -40,11 +40,12 @@ '/', '/remotes', '/version', '/farm', '/metronome', - '/score' + '/score', + '/trace' ], '404' => [ '/this-is-absent', '/wallet/ffffeeeeddddcccc' ] @@ -74,54 +75,78 @@ def test_renders_wallet_pages FakeHome.new.run do |home| FakeNode.new(log: test_log).run(['--ignore-score-weakness', '--standalone']) do |port| wallet = home.create_wallet - test_log.debug("Wallet created: #{wallet.id}") - home = "http://localhost:#{port}" - response = Zold::Http.new(uri: "#{home}/wallet/#{wallet.id}?sync=true", score: nil) + base = "http://localhost:#{port}" + response = Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil) .put(File.read(wallet.path)) assert_equal('200', response.code, response.body) - sleep 0.1 until Zold::Http.new(uri: "#{home}/wallet/#{wallet.id}", score: nil).get.code == '200' + assert_equal_wait('200') { Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil).get.code } [ "/wallet/#{wallet.id}.txt", "/wallet/#{wallet.id}.json", "/wallet/#{wallet.id}/balance", "/wallet/#{wallet.id}/key", "/wallet/#{wallet.id}/mtime", "/wallet/#{wallet.id}/digest", "/wallet/#{wallet.id}.bin", "/wallet/#{wallet.id}/copies" ].each do |u| - res = Zold::Http.new(uri: "#{home}#{u}", score: nil).get - assert_equal('200', res.code, res.body) + assert_equal_wait('200') { Zold::Http.new(uri: "#{base}#{u}", score: nil).get.code } end end end end - # @todo #239:30min This tests is skipped since it crashes sporadically. - # Let's investigate and make it stable. I don't really know what's going - # on, but suspect some collision between threads: - # http://www.rultor.com/t/14940-397702802 def test_pushes_twice - skip FakeNode.new(log: test_log).run do |port| FakeHome.new.run do |home| wallet = home.create_wallet - response = Zold::Http.new(uri: "http://localhost:#{port}/wallet/#{wallet.id}?sync=true", score: nil) - .put(File.read(wallet.path)) - assert_equal('200', response.code, response.body) + base = "http://localhost:#{port}" + assert_equal( + '200', + Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil).put(File.read(wallet.path)).code + ) + assert_equal_wait('200') { Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil).get.code } 3.times do - r = Zold::Http.new(uri: "http://localhost:#{port}/wallet/#{wallet.id}?sync=true", score: nil) + r = Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil) .put(File.read(wallet.path)) assert_equal('304', r.code, r.body) end end end end + def test_pushes_many_wallets + FakeNode.new(log: test_log).run do |port| + base = "http://localhost:#{port}" + FakeHome.new.run do |home| + threads = 20 + done = Concurrent::AtomicFixnum.new + pool = Concurrent::FixedThreadPool.new(threads) + latch = Concurrent::CountDownLatch.new(1) + threads.times do |i| + pool.post do + Thread.current.name = "thread-#{i}" + Zold::VerboseThread.new(test_log).run(true) do + latch.wait(10) + wallet = home.create_wallet + Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil).put(File.read(wallet.path)) + assert_equal_wait('200') { Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil).get.code } + done.increment + end + end + end + latch.count_down + pool.shutdown + pool.wait_for_termination + assert_equal(threads, done.value) + end + end + end + def test_different_logos { '0' => 'https://www.zold.io/images/logo-red.png', '4' => 'https://www.zold.io/images/logo-orange.png', '16' => 'https://www.zold.io/images/logo-green.png' @@ -255,8 +280,29 @@ FakeNode.new(log: test_log).run(['--ignore-score-weakness', '--alias=invalid-alias']) do |port| uri = URI("http://localhost:#{port}/") Zold::Http.new(uri: uri, score: nil).get end end - assert_equal('--alias should be a 4 to 16 char long alphanumeric string', exception.message) + assert(exception.message.include?('should be a 4 to 16 char long alphanumeric string')) + end + + def test_push_fetch_in_multiple_threads + key = Zold::Key.new(text: File.read('fixtures/id_rsa')) + FakeNode.new(log: test_log).run do |port| + FakeHome.new.run do |home| + wallet = home.create_wallet(Zold::Id::ROOT) + base = "http://localhost:#{port}" + Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil).put(File.read(wallet.path)) + assert_equal_wait('200') { Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil).get.code } + cycles = 50 + cycles.times do + wallet.sub(Zold::Amount.new(coins: 10), "NOPREFIX@#{Zold::Id.new}", key) + Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil).put(File.read(wallet.path)) + assert_equal('200', Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}", score: nil).get.code) + end + assert_equal_wait(-10 * cycles) do + Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}/balance", score: nil).get.body.to_i + end + end + end end end