public
Description: StrokeDB is an embeddable distributed document database written in Ruby
Homepage: http://strokedb.com/
Clone URL: git://github.com/yrashk/strokedb.git
oleganza (author)
Sat Jun 07 12:36:04 -0700 2008
commit  65067ffe17c5e1af76a22912a5878d827a070b58
tree    0a23e1083c7fea4a3259c6c8abdf8759b7c708d7
parent  8427e9bcab2a06e6dd3117422d394c1912b09fd0
strokedb / spec / lib / strokedb / sync / store_sync_spec.rb
100644 183 lines (163 sloc) 6.784 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require File.dirname(__FILE__) + '/spec_helper'
 
describe "Store that syncs documents in" do
 
  before(:each) do
    FileUtils.rm_rf TEMP_STORAGES + '/store_sync'
    FileUtils.rm_rf TEMP_STORAGES + '/store_sync_another'
    StrokeDB::Config.build :default => true, :base_path => TEMP_STORAGES + '/store_sync'
    @store = StrokeDB.default_store
    another_cfg = StrokeDB::Config.build :base_path => TEMP_STORAGES + '/store_sync_another'
    @another_store = another_cfg.stores[:default]
  end
 
  after(:each) do
    @store.stop_autosync!
    @another_store.stop_autosync!
    FileUtils.rm_rf TEMP_STORAGES + '/store_sync'
    FileUtils.rm_rf TEMP_STORAGES + '/store_sync_another'
  end
 
  it "should add document that does not yet exist" do
    doc = Document.create!(@another_store, :hello => 'world')
    doc.test = 'passed'
    doc.save!
    sync_rep = @store.sync!(doc.versions.all.reverse)
    @store.find(doc.uuid,doc.previous_version).should == doc.versions.previous
    @store.find(doc.uuid,doc.version).should == doc
    @store.find(doc.uuid).should == doc
    @store.find(doc.uuid).versions.all.should_not include(nil)
    sync_rep.conflicts.should be_empty
    sync_rep.fast_forwarded_documents.should be_empty
    sync_rep.added_documents.should == [doc]
  end
 
  it "should fast-forward document if applicable" do
    doc = Document.create!(@another_store, :hello => 'world')
    doc.test = 'passed'
    doc.save!
    sync_rep = @store.sync!(doc.versions.all.reverse)
    doc_at_store = @store.find(doc.uuid)
    sync_rep.conflicts.should be_empty
    sync_rep.added_documents.should == [doc_at_store]
    sync_rep.fast_forwarded_documents.should be_empty
    doc_at_store.ok = true
    doc_at_store.save!
    another_sync_rep = @another_store.sync!(doc_at_store.versions.all.reverse)
    doc = @another_store.find(doc_at_store.uuid)
    doc.should == doc_at_store
    another_sync_rep.conflicts.should be_empty
    another_sync_rep.added_documents.should be_empty
    another_sync_rep.fast_forwarded_documents.should == [doc]
  end
 
  it "should create non-matching report if applicable" do
    pending "NOT WORKING (refactor store syncing)"
    doc = Document.create!(@another_store, :hello => 'world')
    doc.test = 'passed'
    doc.save!
    adoc = Document.create!(@store, :uuid => doc.uuid, :world => 'hello')
    sync_rep = @store.sync!(doc.versions.all.reverse)
    sync_rep.conflicts.should be_empty
    sync_rep.non_matching_documents.should == [adoc]
    sync_rep.added_documents.should be_empty
    sync_rep.fast_forwarded_documents.should be_empty
  end
 
  it "should do nothing if everything is up-to-date" do
    doc = Document.create!(@another_store, :hello => 'world')
    doc.test = 'passed'
    doc.save!
    sync_rep = @store.sync!(doc.versions.all.reverse)
    sync_rep.conflicts.should be_empty
    sync_rep.non_matching_documents.should be_empty
    sync_rep.added_documents.should_not be_empty
    sync_rep.fast_forwarded_documents.should be_empty
    doc_at_store = @store.find(doc.uuid)
    another_sync_rep = @another_store.sync!(doc_at_store.versions.all.reverse)
    @another_store.find(doc_at_store.uuid).should == doc_at_store
    another_sync_rep.conflicts.should be_empty
    another_sync_rep.non_matching_documents.should be_empty
    another_sync_rep.added_documents.should be_empty
    another_sync_rep.fast_forwarded_documents.should be_empty
  end
 
  it "should create SynchronizationConflict if applicable" do
    doc = Document.create!(@another_store, :hello => 'world')
    doc.test = 'passed'
    doc.save!
    @store.sync!(doc.versions.all.reverse)
    doc_at_store = @store.find(doc.uuid)
    doc_at_store.ok = true
    doc_at_store.save!
    doc.ok = false
    doc.save!
    another_sync_rep = @another_store.sync!(doc_at_store.versions.all.reverse)
    another_sync_rep.non_matching_documents.should be_empty
    another_sync_rep.added_documents.should be_empty
    another_sync_rep.fast_forwarded_documents.should be_empty
    another_sync_rep.conflicts.should_not be_empty
    conflict = another_sync_rep.conflicts.first
    conflict.rev1[0].should == doc_at_store.version
    conflict.rev2[0].should == doc.version
  end
 
  it "should try to resolve SynchronizationConflict if it was created" do
    doc = Document.create!(@another_store, :hello => 'world')
    doc.test = 'passed'
    doc.save!
    @store.sync!(doc.versions.all.reverse)
    doc_at_store = @store.find(doc.uuid)
    doc_at_store.ok = true
    doc_at_store.save!
    doc.ok = false
    doc.save!
    resolve_called = 0
    SynchronizationConflict.module_eval do
      define_method('resolve!') do
        resolve_called += 1
      end
    end
    another_sync_rep = @another_store.sync!(doc_at_store.versions.all.reverse)
    resolve_called.should == 1
  end
 
  it "should add resolution meta to SynchronizationConflict document if it is specified in synchronized document" do
    Object.send!(:remove_const,'SomeStrategy') if defined?(SomeStrategy)
    SomeStrategy = Meta.new
 
    Object.send!(:remove_const,'SomeMeta') if defined?(SomeMeta)
    SomeMeta = Meta.new(:resolution_strategy => SomeStrategy)
 
    # ensure that all metas exist in both stores
    SomeStrategy.document
    SomeMeta.document
    @another_store.sync!(SomeStrategy.document.versions.all.reverse+SomeMeta.document.versions.all.reverse,@store.timestamp)
 
    doc = SomeMeta.create!(@another_store, :hello => 'world')
    doc.test = 'passed'
    doc.save!
    @store.sync!(doc.versions.all.reverse)
    doc_at_store = @store.find(doc.uuid)
    doc_at_store.ok = true
    doc_at_store.save!
    doc.ok = false
    doc.save!
 
    some_strategy_resolve = 0
    SomeStrategy.module_eval do
      define_method("resolve!") do
        some_strategy_resolve += 1
      end
    end
 
    another_sync_rep = @another_store.sync!(doc_at_store.versions.all.reverse)
    conflict = another_sync_rep.conflicts.first
    conflict.should be_a_kind_of(SomeStrategy)
    some_strategy_resolve.should == 1
  end
 
  it "should store original timestamp in synchronization report" do
    original_timestamp = @store.timestamp.counter
    sync_rep = @store.sync!([])
    sync_rep.timestamp.should == original_timestamp
  end
 
  it "should store store's document in synchronization report" do
    sync_rep = @store.sync!([])
    sync_rep.store_document.should == @store.document
  end
 
  it "should update timestamp prior to.sync! if it is specified" do
    original_timestamp = @store.timestamp
    @store.sync!([],100)
    @store.timestamp.counter.should >= 100
    @store.timestamp.should_not == original_timestamp
    original_timestamp = @store.timestamp
    @store.sync!([],LTS.new(200))
    @store.timestamp.counter.should >= 200
    @store.timestamp.should_not == original_timestamp
  end
 
end