Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 59 lines (52 sloc) 1.984 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
require File.join(File.dirname(__FILE__), 'test_helper')

class ShardTest < Test::Unit::TestCase

  def test_activation_should_persist_in_thread
    DataFabric.activate_shard(:city => 'austin')
    assert_equal 'austin', DataFabric.active_shard(:city)
  end
  
  def test_activation_in_one_thread_does_not_change_another
    assert_raises ArgumentError do
       DataFabric.active_shard(:city)
     end
    DataFabric.activate_shard(:city => 'austin')

    Thread.new do
      assert_raises ArgumentError do
         DataFabric.active_shard(:city)
       end
      DataFabric.activate_shard(:city => 'dallas')
      assert_equal 'dallas', DataFabric.active_shard(:city)
    end.join
  end
  
  def test_activations_can_be_nested
    DataFabric.activate_shard(:name => 'Belldandy') do
      DataFabric.activate_shard(:technique => 'Thousand Years of Pain') do
        DataFabric.activate_shard(:name => 'Lelouche') do
          DataFabric.activate_shard(:technique => 'Shadow Replication') do
            assert_equal 'Shadow Replication', DataFabric.active_shard(:technique)
            assert_equal 'Lelouche', DataFabric.active_shard(:name)
          end
          assert_equal 'Thousand Years of Pain', DataFabric.active_shard(:technique)
          assert_equal 'Lelouche', DataFabric.active_shard(:name)
        end
        assert_equal 'Thousand Years of Pain', DataFabric.active_shard(:technique)
        assert_equal 'Belldandy', DataFabric.active_shard(:name)
      end
      assert_raises ArgumentError do
        DataFabric.active_shard(:technique)
      end
      assert_equal 'Belldandy', DataFabric.active_shard(:name)
    end
    assert_raises ArgumentError do
      DataFabric.active_shard(:technique)
    end
    assert_raises ArgumentError do
      DataFabric.active_shard(:name)
    end
  end
  
  def test_activate_shard_returns_result_of_block
    result = DataFabric.activate_shard(:gundam => 'Exia') do
      1234
    end
    assert_equal 1234, result
  end
end
Something went wrong with that request. Please try again.