|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe 'session updating' do |
| 4 | + it 'should support setting a value in the session' do |
| 5 | + post(SESSION_PATH, body: {param1: '5'}) |
| 6 | + json['attributes'].should have_key('param1') |
| 7 | + json['attributes']['param1'].should == '5' |
| 8 | + end |
| 9 | + |
| 10 | + it 'should support updating a value in the session' do |
| 11 | + post(SESSION_PATH, body: {param1: '5'}) |
| 12 | + json['attributes'].should have_key('param1') |
| 13 | + json['attributes']['param1'].should == '5' |
| 14 | + |
| 15 | + put(SESSION_PATH, query: {param1: '6'}) |
| 16 | + json['attributes']['param1'].should == '6' |
| 17 | + end |
| 18 | + |
| 19 | + it 'should support setting a complex values in the session' do |
| 20 | + post(SESSION_PATH, body: {param1: {subparam: '5'}}) |
| 21 | + json['attributes']['param1'].should have_key('subparam') |
| 22 | + json['attributes']['param1']['subparam'].should == '5' |
| 23 | + end |
| 24 | + |
| 25 | + it 'should persist session attributes between requests' do |
| 26 | + post(SESSION_PATH, body: {param1: '5'}) |
| 27 | + get(SESSION_PATH) |
| 28 | + json['attributes']['param1'].should == '5' |
| 29 | + end |
| 30 | + |
| 31 | + it 'should persist updated session attributes between requests' do |
| 32 | + post(SESSION_PATH, body: {param1: '5'}) |
| 33 | + put(SESSION_PATH, query: {param1: '6'}) |
| 34 | + get(SESSION_PATH) |
| 35 | + json['attributes']['param1'].should == '6' |
| 36 | + end |
| 37 | + |
| 38 | + it 'should support removing a value in the session' do |
| 39 | + post("#{SESSION_ATTRIBUTES_PATH}/param1", body: {value: '5'}) |
| 40 | + get(SESSION_ATTRIBUTES_PATH) |
| 41 | + json['keys'].should include('param1') |
| 42 | + |
| 43 | + delete("#{SESSION_ATTRIBUTES_PATH}/param1") |
| 44 | + get(SESSION_ATTRIBUTES_PATH) |
| 45 | + json['keys'].should_not include('param1') |
| 46 | + end |
| 47 | + |
| 48 | + it 'should only update if the session changes' do |
| 49 | + post(SESSION_PATH, body: {param1: '5'}) |
| 50 | + |
| 51 | + # This is not a perfect guarantee, but in general we're assuming |
| 52 | + # that the requests will happen in the following order: |
| 53 | + # - Post(value=5) starts |
| 54 | + # - Post(value=6) starts |
| 55 | + # - Post(value=6) finishes |
| 56 | + # - Get() returns 6 |
| 57 | + # - Post(value=5) finishes |
| 58 | + # Note: this would represent a change from the current persisted value |
| 59 | + # but it does not represent a change from when this request's |
| 60 | + # copy of the session was loaded, so it shouldn't be persisted. |
| 61 | + # - Get() returns 6 |
| 62 | + last_request_to_finish = Thread.new do |
| 63 | + post("#{SESSION_ATTRIBUTES_PATH}/param1", body: {value: '5', sleep: 2000}) |
| 64 | + end |
| 65 | + sleep 1 |
| 66 | + post("#{SESSION_ATTRIBUTES_PATH}/param1", body: {value: '6'}) |
| 67 | + # Verify our assumption that this request loaded it's session |
| 68 | + # before the request Post(value=7) finished. |
| 69 | + json['oldValue'].should == '5' |
| 70 | + get("#{SESSION_ATTRIBUTES_PATH}/param1") |
| 71 | + json['value'].should == '6' |
| 72 | + |
| 73 | + last_request_to_finish.join |
| 74 | + |
| 75 | + get("#{SESSION_ATTRIBUTES_PATH}/param1") |
| 76 | + json['value'].should == '6' |
| 77 | + end |
| 78 | + |
| 79 | + it 'should default to last-write-wins behavior for simultaneous updates' do |
| 80 | + post(SESSION_PATH, body: {param1: '5'}) |
| 81 | + |
| 82 | + # This is not a perfect guarantee, but in general we're assuming |
| 83 | + # that the requests will happen in the following order: |
| 84 | + # - Post(value=7) starts |
| 85 | + # - Post(value=6) starts |
| 86 | + # - Post(value=6) finishes |
| 87 | + # - Get() returns 6 |
| 88 | + # - Post(value=7) finishes |
| 89 | + # - Get() returns 7 |
| 90 | + winner = Thread.new do |
| 91 | + post("#{SESSION_ATTRIBUTES_PATH}/param1", body: {value: '7', sleep: 2000}) |
| 92 | + end |
| 93 | + sleep 1 |
| 94 | + post("#{SESSION_ATTRIBUTES_PATH}/param1", body: {value: '6'}) |
| 95 | + # Verify our assumption that this request loaded it's session |
| 96 | + # before the request Post(value=7) finished. |
| 97 | + json['oldValue'].should == '5' |
| 98 | + get("#{SESSION_ATTRIBUTES_PATH}/param1") |
| 99 | + json['value'].should == '6' |
| 100 | + |
| 101 | + winner.join |
| 102 | + |
| 103 | + get("#{SESSION_ATTRIBUTES_PATH}/param1") |
| 104 | + json['value'].should == '7' |
| 105 | + end |
| 106 | +end |
0 commit comments