Skip to content

Instantly share code, notes, and snippets.

@mgryszko
mgryszko / dyson_jsonp_endpoint.js
Created November 30, 2015 20:50
Render conditionally JSON or JSONP in Dyson
function renderJsonp(res, callback) {
res.append('Content-Type', 'application/javascript');
res.send(callback + '(' + JSON.stringify(res.body) + ');');
}
function renderJson(res) {
res.send(res.body);
}
function render(req, res) {
@mgryszko
mgryszko / ddd_practice.md
Created September 21, 2013 09:14
DDD practice - shipping

Use cases

Book new cargo

Shipping agent provides origin, destination and arrival deadline. Origin and destination as UN locode [1].

System assigns a unique tracking id. Tracking id should be unique and generated by the system.

Cargo is initially unrouted.

Route new cargo

@mgryszko
mgryszko / CaffeineRefreshableCacheTest.kt
Created March 26, 2020 09:19
Characterisation test of Caffeine refreshable cache
import com.github.benmanes.caffeine.cache.CacheLoader
import com.github.benmanes.caffeine.cache.Caffeine
import com.github.benmanes.caffeine.cache.Ticker
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
class CaffeineAsyncRefreshableCacheTest {
@Test
@mgryszko
mgryszko / ParApplicativeTest.kt
Created May 30, 2020 07:13
Arrow ParApplicative mapN combining effects sequentially (unlike parMapN)
import arrow.fx.IO
import arrow.fx.extensions.io.concurrent.concurrent
import arrow.fx.extensions.io.concurrent.dispatchers
import arrow.fx.fix
import kotlin.test.Test
fun f1(): IO<String> = IO {
"f1 ${Thread.currentThread().name}"
}
@mgryszko
mgryszko / BadTestHelpers.java
Last active October 17, 2020 19:58
Bad test helpers
Invoice invoice;
@BeforeEach
void setup() {
createInvoiceToSave();
}
@Test
void standardInvoice() {
mockCustomerFinder();
int id = 12345;
BigDecimal amount = new BigDecimal(100);
Customer customer = new Customer(null, null);
@Test
void standardInvoice() {
when(authenticatedCustomerFinder.apply()).thenReturn(customer);
createInvoiceUseCase.create(id, amount);
class MeteredCacheTest {
Meter gets = new Meter();
Meter puts = new Meter();
Meter hits = new Meter();
MetricRegistry metrics = new MetricRegistry();
Cache delegate = mock<Cache>();
Cache cache = new MeteredCache(delegate, metrics);
@Test
def total(croissants: Pricing, baguettes: Pricing, breadRolls: Pricing): BigDecimal = {
val initialTotal = BigDecimal(0)
val croissantsTotal = initialTotal + total(croissants)
val baguettesTotal = croissantsTotal + total(baguettes)
baguettesTotal + total(breadRolls)
}
def total(pricings: List[Pricing]): BigDecimal =
pricings.foldLeft(BigDecimal(0)) { (grandTotal, pricing) =>
grandTotal + total(pricing)
}
private def total(quantity: Int, tierPrice: TierPrice, unitPrice: UnitPrice): BigDecimal = {
// initial value
var total = BigDecimal(0)
// iteration 1 - tier price
val tiers = quantity / tierPrice.quantity
val tierPriceAmount = tiers * tierPrice.price
// accumulator to pass to the iteration 2
val remainingQuantityAfterTierPrice = quantity % tierPrice.quantity
total = total + tierPriceAmount