Skip to content

Instantly share code, notes, and snippets.

View chbatey's full-sized avatar
🐯
WFH

Christopher Batey chbatey

🐯
WFH
View GitHub Profile
class Datastore {
boolean open() {}
boolean storeField(key, value) {
// I should probably store this
false
}
def close() {}
}
@Test
void testServiceStoresTheValues() {
//given
def mockForDataStore = new MockFor(Datastore)
mockForDataStore.demand.open { true }
mockForDataStore.demand.storeField { k, v -> true }
def mockDataStore = mockForDataStore.proxyInstance()
def service = new Service(mockDataStore)
//when
def processEntry(Map<String, String> someStuffToStore) {
datastore.storeField("this is a made up key","this is a made up value")
}
mockForDataStore.demand.storeField { k, v -> assert v == "someValue"; assert k == "someKey" }
@Test
void testServiceStoresAllTheValues() {
//given
def mockForDataStore = new MockFor(Datastore)
mockForDataStore.demand.open { true }
mockForDataStore.demand.storeField {
k, v ->
if (k == "someKey") assert(v == "someValue")
if (k == "anotherKey") assert(k == "anotherValue")
}
//Java Mocktio
when(someMock.someMethod("something")).thenReturn("oneThing")
when(someMock.someMethod("somethingElse")).thenReturn("anotherThing")
//Groovy MockFor
someMock.demand.someMethodThatReturns {
param1 ->
if (param1 == "something") return "oneThing"
if (param1 == "somethingElse") return "anotherThing"
}
@chbatey
chbatey / Sandbox.scala
Last active December 23, 2015 01:28
Scala + SLF4J + logback + grizzled
package com.batey.examples
import grizzled.slf4j.Logging
class SomeClass extends Logging {
info("Some information")
error("Something terrible")
}
@chbatey
chbatey / cassandraretry.java
Created October 1, 2013 18:43
Cassandra java driver retry policy
public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry);
public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry);
public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry);
@chbatey
chbatey / RetryPolicy.java
Created October 1, 2013 19:26
Overriding retry policy
Cluster cluster = Cluster.builder()
.addContactPoint("127.0.0.1")
.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
.build();
@Test(groups = "unit")
public void shouldReadRetryIfNumberOfRetriesLessThanConfiguredRetries() {
ConsistencyLevel originalConsistency = ConsistencyLevel.ALL;
int numberOfRetries = NUMBER_OF_RETRIES - 1;
RetryPolicy.RetryDecision decision = underTest.onReadTimeout(null, originalConsistency, 1, 1, false, numberOfRetries);
assertEquals(decision.getType(), RetryPolicy.RetryDecision.Type.RETRY, "Should retry");
}