public void Test_Observe() { const string key = "Test_Observe"; var operation = new Observe(key, GetVBucket(), new AutoByteConverter()); var result = IOStrategy.Execute(operation); Console.WriteLine(result.Message); Assert.IsTrue(result.Success); }
/// <summary> /// Authenticates a username and password using a specific <see cref="IConnection"/> instance. The password will /// be encrypted before being sent to the server. /// </summary> /// <param name="connection">An implementation of <see cref="IConnection"/> which represents a TCP connection to a Couchbase Server.</param> /// <param name="username">The username or bucket name to authentic against.</param> /// <param name="password">The password to authenticate against.</param> /// <returns>True if succesful.</returns> public bool Authenticate(IConnection connection, string username, string password) { Username = username; Password = password ?? string.Empty; var temp = connection; Log.Debug(m => m("Authenticating socket {0}", temp.Identity)); var operation = new SaslStart(MechanismType, (VBucket)null, _transcoder, SaslFactory.DefaultTimeout); var result = _ioStrategy.Execute(operation, connection); if (result.Status == ResponseStatus.AuthenticationContinue) { var challenge = result.Message; var reply = ComputeResponse(challenge); operation = new SaslStep(MechanismType, reply, _transcoder, SaslFactory.DefaultTimeout); result = _ioStrategy.Execute(operation, connection); } if (result.Status == ResponseStatus.AuthenticationError) { var tempResult = result; Log.Debug(m => m("Authentication for socket {0} failed: {1}", temp.Identity, tempResult.Message)); } else if (result.Status != ResponseStatus.Success) { var tempResult = result; Log.Debug(m => m("Authentication for socket {0} failed for a non-auth related reason: {1} - {2}", temp.Identity, tempResult.Message, tempResult.Status)); if (operation.Exception != null) { Log.Debug(m => m("Throwing exception for connection {0}", temp.Identity)); throw operation.Exception; } } else { Log.Debug(m => m("Authentication for socket {0} succeeded.", temp.Identity)); } return(result.Status == ResponseStatus.Success); }
public void Test_GetConfig_Non_Default_Bucket() { var saslMechanism = new PlainTextMechanism(_ioStrategy, "authenticated", "secret", new DefaultTranscoder()); _ioStrategy = new DefaultIOStrategy(_connectionPool, saslMechanism); var response = _ioStrategy.Execute(new Config(new DefaultTranscoder(), OperationLifespan, _endPoint)); Assert.IsTrue(response.Success); Assert.IsNotNull(response.Value); Assert.AreEqual("authenticated", response.Value.Name); Console.WriteLine(response.Value.ToString()); }
public void Test_Hello_With_Features_MutationSeqno_And_TcpNodelay_Set() { var features = new List <short>(); features.Add((short)ServerFeatures.MutationSeqno); features.Add((short)ServerFeatures.TcpNoDelay); var hello = new Hello("couchbase-net-sdk/2.1.4", features.ToArray(), Transcoder, 0, 0); var result = IOStrategy.Execute(hello); Assert.IsTrue(result.Success); }
public void When_Key_Does_Not_Exist_Set_Succeeds() { const string key = "Replace.When_Document_Exists_Replace_Succeeds"; //delete the value if it exists var delete = new Delete(key, GetVBucket(), Converter, Transcoder); IOStrategy.Execute(delete); var set = new Set<string>(key, "boo", GetVBucket(), Converter); var result = IOStrategy.Execute(set); Assert.IsTrue(result.Success); }
public void When_MutationSeqno_Is_Not_Set_MutationToken_Is_The_Same_For_All_Instances() { var key = "bar"; var delete = new Delete(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var deleteResult = IOStrategy.Execute(delete); var add = new Add <string>(key, "foo", GetVBucket(), Transcoder, OperationLifespanTimeout); var addResult = IOStrategy.Execute(add); Assert.IsNotNull(addResult.Token); Assert.AreEqual(deleteResult.Token, addResult.Token); }
public void When_Key_Doesnt_Exist_Operation_Succeeds() { const string key = "keythatdoesntexist"; //delete the value if it exists var deleteOperation = new Delete(key, GetVBucket(), new AutoByteConverter(), new DefaultTranscoder(new ManualByteConverter())); var result1 = IOStrategy.Execute(deleteOperation); Console.WriteLine(result1.Message); var operation = new Add <dynamic>(key, new { foo = "foo" }, GetVBucket(), new AutoByteConverter(), new DefaultTranscoder(new ManualByteConverter())); var result = IOStrategy.Execute(operation); Assert.IsTrue(result.Success); }
public void When_Type_Is_String_DateFormat_Is_String() { const string key = "OperationBaseTests.When_Type_Is_String_DateFormat_Is_String"; var set = new Set <string>(key, "somestring", GetVBucket(), Transcoder, OperationLifespanTimeout); var result = IOStrategy.Execute(set); Assert.IsTrue(result.Success); Assert.AreEqual(set.Format, DataFormat.String); var get = new Get <string>(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var getResult = IOStrategy.Execute(get); Assert.IsTrue(getResult.Success); Assert.AreEqual(DataFormat.String, get.Format); Assert.AreEqual(Compression.None, get.Compression); }
public void Test_When_Type_Is_Number_Integrated() { var key = "intkey"; var value = 14.666m; var set = new Set <decimal?>(key, value, GetVBucket(), new AutoByteConverter()); var setResult = IOStrategy.Execute(set); Assert.IsTrue(setResult.Success); Assert.AreEqual(DataFormat.Json, set.Format); var get = new Get <decimal?>(key, GetVBucket(), new AutoByteConverter(), new DefaultTranscoder(new AutoByteConverter())); var getResult = IOStrategy.Execute(get); Assert.AreEqual(DataFormat.Json, get.Format); Assert.IsTrue(getResult.Success); }
public void Test_When_Type_Is_String_Integrated() { var key = "stringkey"; var value = "hiho"; var set = new Set <string>(key, value, GetVBucket(), new AutoByteConverter()); var setResult = IOStrategy.Execute(set); Assert.IsTrue(setResult.Success); Assert.AreEqual(DataFormat.String, set.Format); var get = new Get <string>(key, GetVBucket(), new AutoByteConverter(), new DefaultTranscoder(new AutoByteConverter())); var getResult = IOStrategy.Execute(get); Assert.AreEqual(DataFormat.String, get.Format); Assert.IsTrue(getResult.Success); }
public IOperationResult <T> Send <T>(IOperation <T> operation) { IOperationResult <T> result; try { Log.Debug(m => m("Sending {0} using server {1}", operation.Key, EndPoint)); result = _ioStrategy.Execute(operation); } catch (Exception e) { result = operation.GetResult(); operation.Exception = e; operation.HandleClientError(e.Message); } return(result); }
public void When_Document_Does_Not_Exist_Replace_Fails() { const string key = "Replace.When_Document_Does_Not_Exist_Replace_Fails"; //delete the value if it exists var delete = new Delete(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var result = IOStrategy.Execute(delete); Console.WriteLine(result.Message); //replace it the old doc with a new one var replace = new Replace <dynamic>(key, new { bar = "bar" }, GetVBucket(), Transcoder, OperationLifespanTimeout); var result2 = IOStrategy.Execute(replace); Assert.IsFalse(result2.Success); Assert.AreEqual(ResponseStatus.KeyNotFound, result2.Status); }
public void When_Type_Is_Int_DateFormat_Is_Json() { const string key = "OperationBaseTests.When_Type_Is_Int_DateFormat_Is_Json"; var set = new Set <int?>(key, 100, GetVBucket(), new AutoByteConverter()); var result = IOStrategy.Execute(set); Assert.IsTrue(result.Success); Assert.AreEqual(set.Format, DataFormat.Json); var get = new Get <int>(key, GetVBucket(), new AutoByteConverter(), new DefaultTranscoder(new AutoByteConverter())); var getResult = IOStrategy.Execute(get); Assert.IsTrue(getResult.Success); Assert.AreEqual(DataFormat.Json, get.Format); Assert.AreEqual(Compression.None, get.Compression); }
public void Test_When_Type_Is_String_Integrated() { var key = "stringkey"; var value = "hiho"; var set = new Set <string>(key, value, GetVBucket(), Transcoder, OperationLifespanTimeout); var setResult = IOStrategy.Execute(set); Assert.IsTrue(setResult.Success); Assert.AreEqual(DataFormat.String, set.Format); var get = new Get <string>(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var getResult = IOStrategy.Execute(get); Assert.AreEqual(DataFormat.String, get.Format); Assert.IsTrue(getResult.Success); }
public void Test_When_Type_Is_Number_Integrated() { var key = "intkey"; var value = 14.666m; var set = new Set <decimal?>(key, value, GetVBucket(), Transcoder, OperationLifespanTimeout); var setResult = IOStrategy.Execute(set); Assert.IsTrue(setResult.Success); Assert.AreEqual(DataFormat.Json, set.Format); var get = new Get <decimal?>(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var getResult = IOStrategy.Execute(get); Assert.AreEqual(DataFormat.Json, get.Format); Assert.IsTrue(getResult.Success); }
public void Test_When_Type_Is_Json_Integrated() { var key = "jsonkey"; var value = new { x = "hi", y = 14 }; var set = new Set <dynamic>(key, value, GetVBucket(), Transcoder, OperationLifespanTimeout); var setResult = IOStrategy.Execute(set); Assert.IsTrue(setResult.Success); Assert.AreEqual(DataFormat.Json, set.Format); var get = new Get <dynamic>(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var getResult = IOStrategy.Execute(get); Assert.AreEqual(DataFormat.Json, get.Format); Assert.IsTrue(getResult.Success); }
public void Test_When_Type_Is_Json_Integrated() { var key = "jsonkey"; var value = new { x = "hi", y = 14 }; var set = new Set <dynamic>(key, value, GetVBucket(), new AutoByteConverter()); var setResult = IOStrategy.Execute(set); Assert.IsTrue(setResult.Success); Assert.AreEqual(DataFormat.Json, set.Format); var get = new Get <dynamic>(key, GetVBucket(), new AutoByteConverter(), new DefaultTranscoder(new AutoByteConverter())); var getResult = IOStrategy.Execute(get); Assert.AreEqual(DataFormat.Json, get.Format); Assert.IsTrue(getResult.Success); }
public void Test_When_Type_Is_Binary_Integrated() { var key = "binkey"; var value = new byte[] { 1, 2, 3, 4 }; var set = new Set <byte[]>(key, value, GetVBucket(), Transcoder, OperationLifespanTimeout); var setResult = IOStrategy.Execute(set); Assert.IsTrue(setResult.Success); Assert.AreEqual(DataFormat.Binary, set.Format); var get = new Get <byte[]>(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var getResult = IOStrategy.Execute(get); Assert.IsTrue(getResult.Success); Assert.AreEqual(DataFormat.Binary, get.Format); }
public void Test_When_Type_Is_Binary_Integrated() { var key = "binkey"; var value = new byte[] { 1, 2, 3, 4 }; var set = new Set <byte[]>(key, value, GetVBucket(), new AutoByteConverter()); var setResult = IOStrategy.Execute(set); Assert.IsTrue(setResult.Success); Assert.AreEqual(DataFormat.Binary, set.Format); var get = new Get <byte[]>(key, GetVBucket(), new AutoByteConverter(), new DefaultTranscoder(new AutoByteConverter())); var getResult = IOStrategy.Execute(get); Assert.IsTrue(getResult.Success); Assert.AreEqual(DataFormat.Binary, get.Format); }
public void When_Key_Exists_Set_Succeeds() { const string key = "Replace.When_Document_Exists_Replace_Succeeds"; //delete the value if it exists var delete = new Delete(key, GetVBucket(), Transcoder, OperationLifespanTimeout); IOStrategy.Execute(delete); var add = new Add <string>(key, "foo", GetVBucket(), Transcoder, OperationLifespanTimeout); Assert.IsTrue(IOStrategy.Execute(add).Success); var set = new Set <string>(key, "boo", GetVBucket(), Transcoder, OperationLifespanTimeout); var result = IOStrategy.Execute(set); Assert.IsTrue(result.Success); }
public void Test_Hello_With_Feature_MutationSeqno_Set() { var features = new List <short>(); features.Add((short)ServerFeatures.MutationSeqno); var hello = new Hello("couchbase-net-sdk/2.1.4", features.ToArray(), Transcoder, 0, 0); var result = IOStrategy.Execute(hello); Assert.IsTrue(result.Success); var key = "bar"; var delete = new Delete(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var deleteResult = IOStrategy.Execute(delete); var add = new Add <string>(key, "foo", GetVBucket(), Transcoder, OperationLifespanTimeout); var result2 = IOStrategy.Execute(add); Assert.IsNotNull(result2.Token); }
public void When_Type_Object_Int_DateFormat_Is_Json() { const string key = "OperationBaseTests.When_Type_Object_Int_DateFormat_Is_Json"; var value = new { Name = "name", Foo = "foo" }; var set = new Set <dynamic>(key, value, GetVBucket(), Transcoder, OperationLifespanTimeout); var result = IOStrategy.Execute(set); Assert.IsTrue(result.Success); Assert.AreEqual(set.Format, DataFormat.Json); var get = new Get <dynamic>(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var getResult = IOStrategy.Execute(get); Assert.IsTrue(getResult.Success); Assert.AreEqual(DataFormat.Json, get.Format); Assert.AreEqual(Compression.None, get.Compression); }
public void Test_IncrementOperation() { const string key = "Test_IncrementOperation"; var delete = new Delete(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var result = IOStrategy.Execute(delete); Console.WriteLine("Deleting key {0}: {1}", key, result.Success); var increment = new Increment(key, 0, 1, 0, GetVBucket(), Transcoder, OperationLifespanTimeout); var result1 = IOStrategy.Execute(increment); Assert.IsTrue(result1.Success); Assert.AreEqual(result1.Value, uint.MinValue); increment.Reset(); var result2 = IOStrategy.Execute(increment); Assert.IsTrue(result2.Success); Assert.AreEqual(1, result2.Value); var getOperation = new Get<string>(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var result3 = IOStrategy.Execute(getOperation); var value = result3.Value; Assert.AreEqual(result2.Value.ToString(CultureInfo.InvariantCulture), result3.Value); }
public void When_Key_Exists_GetK_Returns_Value() { var key = "When_Key_Exists_GetK_Returns_Value"; //delete the value if it exists var delete = new Delete(key, GetVBucket(), new DefaultTranscoder(), OperationLifespanTimeout); IOStrategy.Execute(delete); //Add the key var add = new Add <dynamic>(key, new { foo = "foo" }, GetVBucket(), Transcoder, OperationLifespanTimeout); Assert.IsTrue(IOStrategy.Execute(add).Success); var getK = new GetK <dynamic>(key, GetVBucket(), Transcoder, OperationLifespanTimeout); var result = IOStrategy.Execute(getK); Assert.IsTrue(result.Success); var expected = new { foo = "foo" }; Assert.AreEqual(result.Value.foo.Value, expected.foo); }
public void Test_Timed_Execution() { var converter = new AutoByteConverter(); var transcoder = new DefaultTranscoder(converter); var vbucket = GetVBucket(); int n = 1000; //set to a higher # if needed using (new OperationTimer()) { var key = string.Format("key{0}", 111); var set = new Set <int?>(key, 111, vbucket, converter); var get = new Get <int?>(key, vbucket, converter, transcoder); for (var i = 0; i < n; i++) { var result = IOStrategy.Execute(set); Assert.IsTrue(result.Success); var result1 = IOStrategy.Execute(get); Assert.IsTrue(result1.Success); Assert.AreEqual(111, result1.Value); } } }
public void When_Key_Exists_Touch_Returns_Success() { var key = "When_Key_Exists_Touch_Returns_Success"; //delete the value if it exists var delete = new Delete(key, GetVBucket(), Transcoder, OperationLifespanTimeout); IOStrategy.Execute(delete); //Add the key var add = new Add <dynamic>(key, new { foo = "foo" }, GetVBucket(), Transcoder, OperationLifespanTimeout); Assert.IsTrue(IOStrategy.Execute(add).Success); var touch = new Touch(key, GetVBucket(), Transcoder, OperationLifespanTimeout) { Expires = new TimeSpan(0, 0, 0, 3).ToTtl() }; var result = IOStrategy.Execute(touch); Console.WriteLine(result.Message); Assert.IsTrue(result.Success); }
public void Test() { var keyValues = new Dictionary <string, string>(); using (var cluster = new Cluster()) { using (var bucket = cluster.OpenBucket()) { for (int i = 0; i < 10; i++) { var key = "MGETKEY" + i; var value = "{\"val:\"MGETVALUE" + i + "\"}"; bucket.Upsert(key, value); keyValues.Add(key, value); } foreach (var keyValue in keyValues) { Console.WriteLine(bucket.Get <dynamic>(keyValue.Key).Value); } } } var operations = new ConcurrentDictionary <uint, IOperation>(); foreach (var keyValue in keyValues) { var getk = new GetK <dynamic>(keyValue.Key, GetVBucket(), Converter, Transcoder); operations.TryAdd(getk.Opaque, getk); } var noop = new Noop(Converter); operations.TryAdd(noop.Opaque, noop); var results = IOStrategy.Execute <dynamic>(operations); }
public void Test_Observe2() { const string key = "Test_Observe2"; var remove = new Delete(key, GetVBucket(), Converter, Serializer); var set = new Set <int>(key, 10, GetVBucket(), Converter); var result = IOStrategy.Execute(set); Assert.IsTrue(result.Success); var get = new Get <dynamic>(key, GetVBucket(), Converter, Serializer); var result1 = IOStrategy.Execute(get); Assert.IsTrue(result1.Success); Assert.AreEqual(result.Cas, result1.Cas); var operation = new Observe(key, GetVBucket(), new AutoByteConverter()); var result2 = IOStrategy.Execute(operation); Assert.AreEqual(result1.Cas, result2.Value.Cas); Assert.AreEqual(KeyState.FoundPersisted, result2.Value.KeyState); Assert.IsTrue(result2.Success); }
public override IConfigInfo GetConfig(string bucketName, string password) { Log.Debug(m => m("Getting config for bucket {0}", bucketName)); var bucketConfiguration = GetOrCreateConfiguration(bucketName); //if the client is using a password make sure the client configuration references it password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password; if (string.IsNullOrEmpty(bucketConfiguration.Password)) { bucketConfiguration.Password = password; } var exceptions = new List <Exception>(); CouchbaseConfigContext configInfo = null; foreach (var endPoint in bucketConfiguration.GetEndPoints()) { Log.Debug(m => m("Bootstrapping with {0}", endPoint)); IOStrategy ioStrategy = null; try { var connectionPool = ConnectionPoolFactory(bucketConfiguration.PoolConfiguration, endPoint); ioStrategy = IOStrategyFactory(connectionPool); var saslMechanism = SaslFactory(bucketName, password, ioStrategy, Transcoder); ioStrategy.SaslMechanism = saslMechanism; var operationResult = ioStrategy.Execute( new Config(Transcoder, ClientConfig.DefaultOperationLifespan, endPoint)); if (operationResult.Success) { var bucketConfig = operationResult.Value; bucketConfig.SurrogateHost = connectionPool.EndPoint.Address.ToString(); bucketConfig.Password = password; configInfo = new CouchbaseConfigContext(bucketConfig, ClientConfig, IOStrategyFactory, ConnectionPoolFactory, SaslFactory, Transcoder); Log.Info(m => m("{0}", JsonConvert.SerializeObject(bucketConfig))); configInfo.LoadConfig(ioStrategy); Configs[bucketName] = configInfo; break; } var exception = operationResult.Exception; if (exception != null) { exceptions.Add(exception); } //CCCP only supported for Couchbase Buckets if (operationResult.Status == ResponseStatus.UnknownCommand) { throw new ConfigException("{0} is this a Memcached bucket?", operationResult.Value); } Log.Warn(m => m("Could not retrieve configuration for {0}. Reason: {1}", bucketName, operationResult.Message)); } catch (ConfigException) { ioStrategy.Dispose(); Log.Debug(m => m("Bootstrapping with {0} failed.", endPoint)); throw; } catch (AuthenticationException e) { const string msg = "A failure to authenticate may mean that the server has not joined the cluster" + " yet or that the Bucket does not exist. Please check that {0} has joined that" + " cluster and that the Bucket '{1}' exists."; Log.Warn(m => m(msg, endPoint, bucketName)); Log.Warn(e); exceptions.Add(e); } catch (Exception e) { Log.Debug(m => m("Bootstrapping with {0} failed.", endPoint)); Log.Warn(e); exceptions.Add(e); } finally { if (ioStrategy != null && configInfo == null) { ioStrategy.Dispose(); } } } //Client cannot bootstrap with this provider if (configInfo == null) { throw new AggregateException(exceptions); } return(configInfo); }
public IOperationResult <T> Send <T>(IOperation <T> operation) { Log.Debug(m => m("Sending {0} using server {1}", operation.Key, EndPoint)); return(_ioStrategy.Execute(operation)); }