2012-03-23 65 views
2

apply phase of save may fail和/或仍然是正在做異步明年之前不強一致性讀取 - 非祖先查詢。交易影響如何讀取下一個非祖先查詢一致性NDB

基於local testing文章中,我寫了一個測試應該模擬不一致寫着:

import dev_appserver 

dev_appserver.fix_sys_path() 

import unittest 
from google.appengine.ext import ndb 
from google.appengine.ext import testbed 
from google.appengine.datastore import datastore_stub_util 

class SomeModel(ndb.Model): 
    pass 

class SingleEntityConsistency(unittest.TestCase): 
    def setUp(self): 
     # Setup AppEngine env 
     self.testbed = testbed.Testbed() 
     self.testbed.activate() 
     self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=0) 
     self.testbed.init_datastore_v3_stub(consistency_policy=self.policy) 
     self.testbed.init_memcache_stub() 
     # A test key 
     self.key = ndb.Key('SomeModel', 'test') 

    def tearDown(self): 
     self.testbed.deactivate() 

    def test_tx_get_or_insert(self): 
     p = SomeModel.get_or_insert('test') 
     self.assertEqual(0, SomeModel.query().count(1), "Shouldn't be applied yet") 
     self.assertEqual(1, SomeModel.query(ancestor=self.key).count(1), "Ancestor query read should be consistent") 

    def test_no_tx_insert(self): 
     p = SomeModel(id='test') 
     p.put() 
     self.assertEqual(0, SomeModel.query().count(2), "Shouldn't be applied yet") 
     self.assertEqual(1, SomeModel.query(ancestor=self.key).count(1), "Ancestor query read should be consistent") 

    def test_with_ancestor(self): 
     p = SomeModel(id='test') 
     p.put() 
     self.assertEqual(p, SomeModel.query(ancestor=self.key).get()) 

    def test_key(self): 
     p = SomeModel(id='test') 
     p.put() 
     self.assertEqual(p, self.key.get()) 

if __name__ == '__main__': 
    unittest.main() 

實際問題...

  1. 是否包裹在開頭所述交易改變行爲put()?我還需要一個強烈一致的查詢作出肯定,我會讀是寫在TXN? (測試表明,我仍然需要強烈一致的查詢)

  2. key.get()認爲是強一致? (測試表明,它是)

UPDATE

我已經更新了測試代碼圭多提到,現在所有的測試合格:

self.testbed.init_datastore_v3_stub(consistency_policy=self.policy) 

回答

4

我相信你必須做一些事情來激活該政策。這將解釋測試失敗。此外,我相信只有查詢受到影響,並且唯一的投放是有效的交易。最後要提防NDB的緩存。

+0

如果一個孤獨的賣出期權實際上是一種交易是否意味着相同的實體組中的所有看跌期權獲得序列上的單個鎖? – 2012-03-30 19:26:02