2013-03-27 42 views
0

此測試用例將在第一次通過,但接下來將失敗。我不知道爲什麼在第二次執行插入方法後沒有將列值更新爲「value1」。無法通過節儉api更新列值

[TestMethod] 
    public void TestMethod() 
    { 

     client.set_keyspace(KEYSPACE); 

     byte[] key = utf8Encoding.GetBytes("123456789"); 

     ColumnParent parent = new ColumnParent(); 
     parent.Column_family = "Users"; 

     Column column = new Column(); 
     column.Name = utf8Encoding.GetBytes("columnname1"); 
     column.Timestamp = DateTime.Now.Millisecond; 
     column.Value = utf8Encoding.GetBytes("value1"); 

     // insert 
     client.insert(key, parent, column, ConsistencyLevel.ONE); 

     ColumnPath path = new ColumnPath(); 
     path.Column_family = "Users"; 
     path.Column = utf8Encoding.GetBytes("columnname1"); 

     // search 
     ColumnOrSuperColumn returnedColumn = client.get(key, path, ConsistencyLevel.ONE); 
     Assert.AreEqual("value1", utf8Encoding.GetString(returnedColumn.Column.Value)); 

     // update 
     column.Timestamp = DateTime.Now.Millisecond; 
     column.Value = utf8Encoding.GetBytes("value2"); 
     client.insert(key, parent, column, ConsistencyLevel.ONE); 

     returnedColumn = client.get(key, path, ConsistencyLevel.ONE); 
     Assert.AreEqual("value2", utf8Encoding.GetString(returnedColumn.Column.Value)); 

回答

-1

如果您在代碼中連續調用兩次,您需要確保您的時間戳正在遞增。使用微秒分辨率並創建一個函數,該函數記錄上次返回的時間,並在下一次小於或等於上次返回時添加一個。

只有使用毫秒分辨率,所有時間標記可能都是相同的,所以卡桑德拉回複比較字節,所以value2獲勝。

+0

感謝您的幫助。我使用了私有靜態只讀DateTime Jan1st1970 = new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc); \t \t公共靜態長的currentTimeMillis \t \t { \t \t \t得到{(長)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds; } \t \t},並解決此問題。 – 2013-03-27 06:10:41