Created
November 8, 2019 12:07
-
-
Save smallufo/e0a92f6284e9f65bd0d79eafe73bf1f6 to your computer and use it in GitHub Desktop.
Delay test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// delay ISGD more | |
class IsgdImpl : UrlShorter { | |
override suspend fun getShortUrl(longUrl: String): String? { | |
logger.info("running : {}", Thread.currentThread().name) | |
val url = "https://is.gd/create.php?format=simple&url=%s".format(URLEncoder.encode(longUrl, "UTF-8")) | |
return withContext(Dispatchers.IO) { | |
logger.info("running Dispatchers.IO : {}", Thread.currentThread().name) | |
Request.Get(url).also { | |
delay(5000) | |
}.execute() | |
.also { | |
logger.info("ISGD network call invoked") | |
}.returnContent().asString().also { | |
logger.info("returning {}", it) | |
} | |
} | |
} | |
} | |
class TinyImpl : UrlShorter { | |
override suspend fun getShortUrl(longUrl: String): String? { | |
logger.info("running : {}", Thread.currentThread().name) | |
val url = "http://tinyurl.com/api-create.php?url=$longUrl" | |
return withContext(Dispatchers.IO) { | |
logger.info("running Dispatchers.IO : {}", Thread.currentThread().name) | |
Request.Get(url).also { | |
delay(1000) | |
}.execute() | |
.also { | |
logger.info("TINY network call invoked") | |
}.returnContent().asString().also { | |
logger.info("returning {}", it) | |
} | |
} | |
} | |
} | |
/** | |
20:02:24,579 INFO NullImpl - running : main | |
20:02:24,583 INFO DumbImpl - running : main | |
20:02:24,584 INFO IsgdImpl - running : main | |
20:02:24,590 INFO TinyImpl - running : main | |
20:02:24,590 INFO IsgdImpl$getShortUrl$2 - running Dispatchers.IO : DefaultDispatcher-worker-1 | |
20:02:24,598 INFO TinyImpl$getShortUrl$2 - running Dispatchers.IO : DefaultDispatcher-worker-3 | |
20:02:26,615 INFO TinyImpl$getShortUrl$2 - TINY network call invoked | |
20:02:26,619 INFO TinyImpl$getShortUrl$2 - returning http://tinyurl.com/389lo | |
20:02:26,640 INFO UrlShorterServiceTest$testHedging$1 - result = http://tinyurl.com/389lo | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Delay TINY more | |
class IsgdImpl : UrlShorter { | |
override suspend fun getShortUrl(longUrl: String): String? { | |
logger.info("running : {}", Thread.currentThread().name) | |
val url = "https://is.gd/create.php?format=simple&url=%s".format(URLEncoder.encode(longUrl, "UTF-8")) | |
return withContext(Dispatchers.IO) { | |
logger.info("running Dispatchers.IO : {}", Thread.currentThread().name) | |
Request.Get(url).also { | |
delay(1000) | |
}.execute() | |
.also { | |
logger.info("ISGD network call invoked") | |
}.returnContent().asString().also { | |
logger.info("returning {}", it) | |
} | |
} | |
} | |
} | |
class TinyImpl : UrlShorter { | |
override suspend fun getShortUrl(longUrl: String): String? { | |
logger.info("running : {}", Thread.currentThread().name) | |
val url = "http://tinyurl.com/api-create.php?url=$longUrl" | |
return withContext(Dispatchers.IO) { | |
logger.info("running Dispatchers.IO : {}", Thread.currentThread().name) | |
Request.Get(url).also { | |
delay(5000) | |
}.execute() | |
.also { | |
logger.info("TINY network call invoked") | |
}.returnContent().asString().also { | |
logger.info("returning {}", it) | |
} | |
} | |
} | |
} | |
/** | |
20:05:54,711 INFO NullImpl - running : main | |
20:05:54,719 INFO DumbImpl - running : main | |
20:05:54,719 INFO IsgdImpl - running : main | |
20:05:54,728 INFO IsgdImpl$getShortUrl$2 - running Dispatchers.IO : DefaultDispatcher-worker-1 | |
20:05:54,729 INFO TinyImpl - running : main | |
20:05:54,738 INFO TinyImpl$getShortUrl$2 - running Dispatchers.IO : DefaultDispatcher-worker-4 | |
20:05:57,253 INFO IsgdImpl$getShortUrl$2 - ISGD network call invoked | |
20:05:57,257 INFO IsgdImpl$getShortUrl$2 - returning https://is.gd/EuvYes | |
20:05:57,282 INFO UrlShorterServiceTest$testHedging$1 - result = https://is.gd/EuvYes | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment