Skip to content
Browse files

Merge branch 'master' of github.com:Netflix/netflix.github.com into g…

…h-pages
  • Loading branch information...
2 parents 1abd882 + 493e8f9 commit 9a473d7795c32a3702fa3c20316b8fbe52722c8a @Randgalt Randgalt committed Mar 8, 2013
Showing with 42 additions and 17 deletions.
  1. +38 −0 blogs/mockwebserver.html
  2. +1 −1 index.html
  3. +1 −0 js/models.js
  4. +2 −16 js/nf.js
View
38 blogs/mockwebserver.html
@@ -0,0 +1,38 @@
+<h1>Why mock servers that don't mock matter</h1>
+Operations folk can relate to the idea that best intentions, and cleverest libraries, do not a stable system make. &nbsp;More recently in <a href="http://techblog.netflix.com/2013/03/denominator-multi-vendor-interface-for.html">denominator</a>, I've relied on the ability to check what's going onto the wire as a foundation for crafting http api requests. &nbsp;A <a href="https://twitter.com/adrianfcole/status/309152829778575361">quick poll</a> suggests I should say more.<br />
+<br />
+When using libraries or otherwise creating http requests, a false sense of security is your worst nightmare. &nbsp;For example,&nbsp;in java 6&nbsp;<code>Expect</code> is a <a href="http://javasourcecode.org/html/open-source/jdk/jdk-6u23/sun/net/www/protocol/http/HttpURLConnection.java.html">restricted header</a>, eventhough it isn't <a href="http://www.docjar.com/html/api/sun/net/www/protocol/http/HttpURLConnection.java.html">afterwards</a>. &nbsp;Typical mock tests cannot prove your application can work when they test above a&nbsp;<a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">library or runtime</a>&nbsp;that can change. &nbsp;These changes occur more than you think, especially with <a href="http://www.oracle.com/technetwork/java/eol-135779.html">Java 6 EOL</a> migrations and Java 8 around the corner.<br />
+<br />
+<a href="http://code.google.com/p/mockwebserver/">mockwebserver</a>&nbsp;is a great way to setup truths that your application must adhere to, without regard to how it is crafted. &nbsp;Despite its name, mockwebserver literally reads bytes off the wire to assert the expected behavior. &nbsp;Unlike many libraries, but <a href="http://stackoverflow.com/questions/393099/mocking-http-server">similar to some</a>, mockwebserver is only several small classes with one <a href="http://code.google.com/p/mockwebserver/source/browse/trunk/src/main/java/com/google/mockwebserver/MockWebServer.java">main controller</a>. &nbsp;The small code base is easy to read when you are in the middle of debugging a gnarly issue.<br />
+<br />
+Case in point: in <a href="https://github.com/Netflix/denominator">denominator</a>, I want to assure any aggregate commands needed for a dns change are ordered and have the expected result. &nbsp;In a <a href="https://github.com/Netflix/denominator/blob/master/providers/denominator-route53/src/test/java/denominator/route53/Route53ResourceRecordSetApiMockTest.java">mock test</a>, setup tests like below to make assert that behavior. &nbsp;The only requirement is that my code can accept a url.<br />
+<br />
+<pre>@Test
+public void addSecondRecordRecreatesRRSetAndRetainsTTL() throws IOException, InterruptedException {
+ MockWebServer server = new MockWebServer();
+ // enqueue responses your code expects. Too few here and your test will hang!
+ server.enqueue(new MockResponse().setResponseCode(200).setBody(oneRecord));
+ server.enqueue(new MockResponse().setResponseCode(200).setBody(changeSynced));
+ server.play();
+
+ try {
+ // ensure your constructors can take a url so that you can wire in mockwebserver
+ Route53ResourceRecordSetApi api = new Route53ResourceRecordSetApi(mockRoute53Api(server.getUrl("/").toString()));
+ api.add(a("www.foo.com.", "5.6.7.8"));
+ } finally {
+ // *optionally* check the requests you sent.
+ // I skip this when I enqueue errors to validate exception messages
+ RecordedRequest listNameAndType = server.takeRequest();
+ assertEquals(listNameAndType.getRequestLine(),
+ "GET /2012-02-29/hostedzone/Z1PA6795UKMFR9/rrset?name=www.foo.com.&amp;type=A HTTP/1.1");
+
+ RecordedRequest createRRSet = server.takeRequest();
+ assertEquals(createRRSet.getRequestLine(), "POST /2012-02-29/hostedzone/Z1PA6795UKMFR9/rrset HTTP/1.1");
+ assertEquals(new String(createRRSet.getBody()), replaceWith2ElementRecordSet);
+
+ server.shutdown();
+ }
+}
+</pre>
+<br />
+Tests written in mockwebserver, applied across a number of JREs, will bullet-proof your tests for your supported configurations. &nbsp;In summary, it doesn't really matter if you choose to use mockwebserver or something else to assure your http traffic shapes as expected. &nbsp;Main idea is to be strict so that regardless of runtime shifts, you know what your application sends and what it is resilient to.
View
2 index.html
@@ -41,7 +41,7 @@
</script>
<script type="text/javascript" src="http://githubproxy-942908148.us-east-1.elb.amazonaws.com/REST/v2/consolidated?jsonp=repos"></script>
<script type="text/javascript" src="js/models.js?v=201303071100"></script>
- <script type="text/javascript" src="js/nf.js?v=201303071100"></script>
+ <script type="text/javascript" src="js/nf.js?v=201303072325"></script>
</head>
<body>
View
1 js/models.js
@@ -51,6 +51,7 @@ var tabs = [
];
var blogs = [
+ new BlogSpec('mockwebserver', 'March 8, 2013', "Why mock servers that don't mock matter"),
new BlogSpec('meetup-2', 'March 7, 2013', 'NetflixOSS Meetup - Season 1, Episode 2')
];
View
18 js/nf.js
@@ -111,7 +111,7 @@ function showTab(which)
for ( var i = 0; i < tabs.length; ++i )
{
var item = tabs[i];
- if ( which.match(item.code + '.*') )
+ if ( which && which.match(item.code + '.*') )
{
if ( item.handler )
{
@@ -382,27 +382,13 @@ function buildTabs()
$('#sub-header').html(content);
}
-function getTabIndex(which)
-{
- var index = null;
- for ( var i = 0; i < tabs.length; ++i )
- {
- if ( which.match(tabs[i].code + '.*') )
- {
- index = i;
- break;
- }
- }
- return index ? index : 0;
-}
-
function getViewParam()
{
if ( location.hash )
{
return location.hash.substring(1);
}
- return $.urlParam('view');
+ return tabs[0].code;
}
function refineBlogHash(hash)

0 comments on commit 9a473d7

Please sign in to comment.
Something went wrong with that request. Please try again.