Scala with Mockito gotcha

We do a lot of Scala over at Top10 nowadays.  We also do a lot of unit testing and that means a lot of mocking.  We’ve been using Mockito due to our familiarity with it as Java developers and because Scala’s excellent interoperability with Java means that features like the @Mock annotation still work on Scala objects.

One thing to bear in mind when using Mockito matchers with Scala is that Mockito, being a Java library knows nothing of a Scala method’s default arguments.  So if you have a method like:

def myMethod(param: String, defaultStr: String = "defaultToThis")

and you try to mock it thusly:

when(mocked.myMethod(anyString))

you’ll probably be met with the following complaint from Mockito:

[info]   org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers!
[info] 0 matchers expected, 2 recorded.
[info] This exception may occur if matchers are combined with raw values:
[info]     //incorrect:
[info]     someMethod(anyObject(), "raw String");
[info] When using matchers, all arguments have to be provided by matchers.
[info] For example:
[info]     //correct:
[info]     someMethod(anyObject(), eq("String by matcher"));

You can’t mix regular values with Mockito matchers when mocking a method’s behavior and the default values will be supplied as plain old values to the mockito object.  The only option is to call all the default parameters with matchers:

when(mocked.myMethod(anyString, anyString))

or if you want to be more specific (and generally you should):

when(mocked.myMethod(anyString, Matchers.eq("defaultToThis")))

(This highlights another gotcha.  eq is a method on AnyRef so it hides any static import of Matchers.eq).

Posted in Development, Java, Scala | Tagged , , , , | 1 Comment

Leaving the BBC. Joining Top10

After ~20 months it’s time for me to leave the Big British Castle. It’s been an honour to work for such a fantastic organisation which is very close to my heart. I’ve got to work on some great projects and with an awesome bunch of very smart people.

I won’t go into my reasons for leaving other than to say that I’m super excited to be starting work at Top10 tomorrow! The name of the site speaks for itself as does the subtitle on the front page “The Top 10 of Everything, Created by Everyone”.

I love the concept of the site and where the guys are taking it. Top10 will be the first startup I’ve joined and I can’t wait to start working on the site with some new technologies and another great team.

Here are my Top 10s.

Posted in BBC, top10 | Tagged , , | Leave a comment

Most tweeted Columbo episodes

Sadly, Peter Falk died this week.  This means a lot of people are tweeting about Columbo.  Instead of doing all the things I ought to be doing today like exercising, working on an upcoming project and feeding myself I wrote a Python script to find the most tweeted about Columbo episodes.  Here are the results:

The script searches for the episode title in quotes plus the word Columbo (as episodes like “Undercover” and “Dead Weight” were returning a lot of false positives).  I’ll stick the script on github later, first I have to get to the supermarket before it closes.

Update: Script is on github.  It’s dead simple, probably could have just been a gist.  I got the list of episodes from here.  There are some sample results in the git project.

Posted in Development, Fun, Python, Twitter | Tagged , , , , , | Leave a comment

Twitter rate limiting and Google App Engine

Twitter blocked icon@markovator has been silent for some time. He’d been replying to people but he’d not been able to come up with his own original tweets. The reason for this is the that he was being rate limited by Twitter. Initially this seemed strange to me as markovator tweets quite infrequently (at the very most once every minute if people are constantly pestering him). However the markovator code was inconsistent in how it authenticated with the twitter api. When it didn’t seem necessary to authenticate I didn’t (for example when requesting an unprotected user’s tweets). Twitter applies the rate limit to the IP of the machine making the request and when you’re running on Google App Engine you never know anything about the node you’re making http requests from or who else is using it. Whilst App Engines IPs seem to have been whitelisted (they can make 20000 requests per hour) all it would take (and did take, it seems) to cause markovator’s requests to be refused is a few more apps with very heavy unauthenticated twitter usage.

So you’ve been warned. If you’re building an app the uses twitter on app engine always OAuth authenticate, even when this seems unnecessary or a pain otherwise you’ll always be at the whim of twitter’s IP limiting. Obviously markovator always authenticates as @markovator now. If you usually authenticate on behalf of your users then you should use the twitter account you used to register your twitter app just for making those public, read only requests you might have thought could go unauthenticated.  You can avoid the OAuth flow by getting a single access token for that account from your twitter app page on dev.twitter.com

On a side note the account/rate_limit_status endpoint seems a little capricious. I have an admin endpoint that reports the status of the markovator app, including it’s authenticated rate limit status. When run locally it returns 350 per hour whilst when run on app engine it returns 20000 per hour.  Perhaps the whitelisted status of App Engine overrides the fact that the status request is authenticated.  Despite this the authenticated rate limiting does seem to apply when making authenticated requests from App Engine to Twitter.

Links:

Posted in Development, markovator, Python | Tagged , , , , | Leave a comment

Minecraft Tumblr blog

Postcards from Minecraft

I created a Tumblr blog for recording our minecraft adventures rather than filling this site up.

I’ve cross posted the original minecraft post from here and added a couple more.

More to come soon. Enjoy :)

Posted in Fun, Gaming, Minecraft | Tagged , , | 1 Comment

Minecraft

Caragh looking out at the monsters from the castle wallsI’ve been playing Minecraft single player for a while now and it’s great.  Everything you’ve heard about the game is true.  The whole mechanic hinges around the simple rules that govern the physics of the world and the unique experiences that emerge from them (apparently this is called emergent gameplay).  The adventures you can have battling monsters, exploring caves and going off on hikes and sea voyages can be exhilarating, scary and challenging.  The simple graphics are charming and sometimes quite awe inspiring once you reach a mountain peak and get a good view of the impressive terrains that get generated.  There’s nothing like building your own world to really get you invested in what goes on in it and that’s exactly what you do as you stack tons of cobblestones into towering battlements around your castle keep, dig endlessly downwards into your labyrinth mines and caves and carefully landscape grassy forests and deserts into the perfect back garden.

Singleplayer was great but what has been even more fun is setting up a server and convincing Caragh to join me in a multiplayer world.  Here are some screenshots of the action we’ve encountered so far.

Minecraft beach/desert screenshot

This is the spawn location in our world, every time you die you’re yanked back here and all your stuff stays where you were so you have to run back and collect it before it disappears.

Screenshot Caragh of cutting down trees

Caragh’s cutting down logs while I head off to find coal.  It’s important to gather coal and wood on your first day so you can craft torches to get through that long first night.

Sun's going down, here come the monsters

The sun’s going down which means the monsters will be coming out.  Time to start to build a shelter so we can hide away.

Time to dig a shelter into the hillside

Got our chest, crafting bench and furnace set up

We’ve got a little hole to hide away in now, with a door, a furnace, crafting bench, a chest and most importantly, torch light.

Started digging a mine

At first the only thing to do at night is to start digging a mineshaft so you can mine out some raw materials like coal, iron and maybe even diamond.

A scary cave to explore at the bottom of our mineWhilst mining you’ll often breakthrough into huge natural caverns or tight twisting passages with zombies and skeletons hiding around every corner.  It’s up to you whether to hastily brick them up or to grab a torch and a sword and venture in.

I think Caragh got Creepered!

Caragh standing in a creeper crater.  Creepers will sneak up on you soundlessly before making a hissing noise and exploding.  Creeper death was our number two cause of death starting out, after falling down our mineshaft.

Finally we have a small farm and some safe walls

Eventually we get a nice set of castle walls up outside our shelter so we can come out at night in relative safety.  The plants down there are the start of a wheat farm and a sugar cane crop.I built a porch out the front

We built a porch out the front because we kept opening our front door to find a creeper or a spider waiting for us.  The elevated porch is harder for the creepers to get as near to and the glass allows us to look out and see where they are.

Looking nicer inside, we have cake!

We expanded the inside around our mine a little bit and made cake.  The cake replenishes health and is made from the crops we farmed outside (and some eggs and milk).  We found the pumpkin outside and put a torch inside to make a Jack-O-Lantern.

I climbed a big tree on the mountainside to get a better view of our castle

I climbed a tall tree on the hillside to get a better view of our castle/shelter.  You can see the skylight that lights our mineshaft from here.

Venturing further from home

Venturing even further from home.

Begging to be explored!

In the other direction is a huge island.  I can’t wait to take a boat over there and explore.

Posted in Gaming, Minecraft | Tagged , , | 7 Comments

A History of the World in 100 Seconds

I attended the History Hackday at the Guardian and helped Gareth implement his cunning plan to scrape Wikipedia for all historical events with associated geo coordinates and present them chronologically.  The video above is the result.  Unfortunately I couldn’t be there on Sunday but whilst Gareth set about doing the hard bit, using Python and Mongo to extract the events from his dump of Wikipedia, I managed to code up a framework in Java for Gareth to produce the visualisation video from.  The dataset is pretty fuzzy, we went through each Wikipedia year page and kept all pages that were linked to for that year that also had coords.  I’m pretty pleased with the results, we won a prize for “best visualisation”, although I’d like to spend a little more time tweaking the graphics if I find the time.  The code is available on github.

The hackday itself was pretty cool, well stocked with beer, pizza and cool people. Props for a well organised event go to @fidothe, I’m sorry I couldn’t have been there for the whole thing.

Posted in Development, Java, Python, Wikipedia | Tagged , , , , | 2 Comments

Neon Trails Live Wallpaper for Android

Since version 2.1, Android has had a live wallpaper feature which allows dynamic wallpapers to be built for the Android home screen that can animate and respond to touch and other inputs.  They’re remarkably easy to develop so I spent some of my time off this holiday season building one I call Neon Trails.  The wallpaper draws little trails that wind themselves across the screen and around each other.Neon Live Wallpaper

The algorithm for the trails is relatively simple, each moves forward one unit and then chooses a random direction to travel in before moving forward another unit and picking another direction.  Each trail won’t pick a direction that will cause it to overlap with itself or another and will stop moving as soon as is it gets trapped.

The rendering was done using the 2D API that Android provides via the Canvas class.  The little lights at the end of active trails were achieved by faking a bloom effect by repeatedly rendering a circle with a number of BlurMaskFilters, each with a smaller radius than the last.

Neon Trails Live Wallpaper for Android with a green color scheme

Most of the work was spent optimising various things.  A more optimised wallpaper will scroll better as the user drags between virtual home screens and will use less battery.  Memory allocation and garbage collection are generally quite expensive on the Dalvik VM so avoiding using the new keyword where ever possible, except on start up, is generally a good idea when optimising for Android (there is still some work to do here as both the Trail and Grid class frequently allocate new Point instances).  The DDMS eclipse tool that you get with Android is excellent for tracking these things.

The technique that seemed to give the greatest benefit in terms of performance was generating an offscreen buffer (an instance of Bitmap) to render the “terminated” trails to (active trails are moving too much so would invalidate the buffer on every frame).  Referencing the buffer with a SoftReference meant that the system could always garbage collect the image if it couldn’t spare the memory required.  This is generally good practice for all non-essential offscreen buffers when working on optimised UIs in Java where memory might be tight.

Neon Trails Live Wallpaper for Android with yellow color scheme

The wallpaper is fairly customisable in terms of appearance, you can alter the color scheme and appearance of the trails and can even set a lower frame rate if you’re worried about the wallpapers effect on battery life.  The wallpaper is available for free on the Android Market and you can find the code on Github.

QR Code for Neon Trails Live Wallpaper for Android

QR Code for Neon Trails Live Wallpaper for Android

Posted in Android, Development | Tagged , , , | Leave a comment

Markovator – Python + Google App Engine + Markov chains + Twitter

Can good TV shows have a dog eating cat poop.
@markovator_dev
Markovator Dev

As a child a chat bot program called NIALL made quite an impression on me.  It came on one of those demo floppy disks from some PC magazine I forget the name of now.  NIALL stands for Non Intelligent Acquired Language Learner and what it (he?) would do is remember each word you “said” to it and the frequency that any following word appeared after it.  So for example when given the sentence “the cat sat on the mat” NIALL would remember that “cat” always precedes “sat” but that “the” only precedes “cat” half the time, the other half of the time it precedes “mat”.  Whilst slowly building up this dataset of word frequencies with each message you would type in it would use them to generate its own nonsensical, sometimes amusing replies.  You can find a better description of the process on the how it works page of this GNOME implementation.

Continue reading

Posted in Development, Python | Tagged , , , , | 1 Comment

HeyGraph Javascript and canvas graphing tool

HeyGraph Screenshot

To learn a bit about the HTML 5 canvas I’ve been writing a simple graphing visualization tool in Javascript.  Continue reading

Posted in Development, Javascript | Tagged , , , , , , , | Leave a comment