I was recently adding a feature to an internal web UI that caught all unhandled JavaScript errors and reported them to the backend service. The implementation went smoothly with most of the effort spent figuring out how to test the code that was reporting the errors.
If the error reporting failed, I didn’t want to trigger reporting another error or completely lose that error. I decided to log a reporting error to the console. I wanted to write a test showing that errors reporting errors were handled so that a future me, or another developer, didn’t accidentally remove this special error handling and enable a never ending cycle of of reporting failed reporting attempts.
It took me a while to figure out how to do this. I searched the web and found various articles about using Jasmine to do async tests. They were helpful but I also wanted to mock out a function, console.error, and assert that it was called. None of the examples I found were explicit about doing something like this. I forget how many different approaches I tried, but it took a while to figure out the below solution.
It takes an incoming event object and merges it with a default value and posts that to the backing service. fetch returns a Promise and the code handles errors by calling catch on it and logging.
Below is what I eventually came up with for testing the error handling feature of reportEvent.
This uses spyOn to mock out fetch and console.error. The fetch call is told to return a rejected Promise. The console.error spy is a bit more interesting.
The console.error spy is told to call a fake function. That function asserts that the console.error spy has been called. More importantly, it also calls a done function. That done function is a callback passed to your test by Jasmine. Calling done signals that your async work is completed.
If done is never called then Jasmine will fail the test after some amount of time. By calling done in our console.error fake, we’re able to signal to Jasmine that we’ve handled the rejected promise.
You don’t actually need the expect(console.error).toHaveBeenCalled(); as done won’t be called unless console.error has been called. If you don’t have it though then Jasmine will complain there are no assertions in the test.
So there we have it, an example of using some of Jasmine’s asynchronous test features with spies. I wish I had found an article like this when I started this task. Hopefully it saves you, and future me, some time.
Leiningen test selectors are great. They allow you to filter what tests run by applying a function to the test’s metadata. If that function returns a truthy value then that test will run. lein-test-refresh supports them and even includes a built in one for its focus feature.
I was recently asked if test-refresh could support filtering tests using a regular expression against the name of a namespace or test. Lucky for me, test-refresh already supports this because of its support of test selectors.
Most of the examples of Leiningen test selectors show very simple functions that look for the existence of a keyword in the metadata. We can do more than that. We can write a predicate that does whatever we want with the metadata.
To take a look at a test’s metadata, I generated a new project and looked at the generated default test file.
1234567
(ns selector.core-test(:require[clojure.test:refer:all][selector.core:refer:all]))(deftesta-test(testing"FIXME, I fail."(is(= 01))))
I then used my repl and to see what metadata was on the test.
You could write the above code is many different ways. Whatever you write, it needs to look for the existence of integration in either the test’s name or namespace.
If you wanted to make lein test or lein test-refresh only run non-integration tests you can add a default test selector to the project.clj.
Enjoy! I hope this example helps you run a subset1 of your Clojure tests through Leiningen test selectors.
Running a subset of your tests can be helpful and test-refresh has a few features that help you do that. If you can, I’d still recommend making all your tests fast enough to run them all the time.↩
The setup linked above works great for when I’m doing work all by myself. It showed a problem when using ssh and tmux to pair with another developer. Instead of both developers receiving a notification, only one did. One is better than none but not ideal.
Below is a GIF showing the problem. Each window simulates a different developer.
This wasn’t too hard to fix. A little digging through the tmux manpage shows that tmux display-message takes an optional flag telling it which client receives the message. If we can get a list of all the clients then iterating over them and sending a message to each is straightforward.
tmux list-clients give us this list. Below is the output.
What we care about are the parts that look like /dev/ttys002. At first I used cut to grab these values but then I dug a bit deeper into the tmux manpage.
It turns out that you can specify a format to tmux list-clients. Running tmux list-clients -F "#{client_name}" gives us the output we care about.
We can combine that with xargs to send a message to multiple clients.
That command is a bit much to put into lein-test-refresh’s configuration so I shoved it in a script called notify and configured lein-test-refresh to use it. Script and GIF of that below. Now both you and your pair can get notifications.
At the beginning of every year I like to take the time to reflect on my previous year’s reading. It gives me a time to correct my data and think about where I want my reading to go in the upcoming year.
Here are links to my previous end-of-year reflections: 2013, 2014, 2015, 2016, and 2017.
I’ve continued to keep track of my reading using Goodreads. My profile continues to have the full list of the books I’ve read since 2010. Here is my entire 2018 record.
I slacked off a bit when writing reviews for all of my read books in Goodreads. I often didn’t write a review until some time had passed after completing the book and, as a result, I think I did a worse job reviewing books. Some books don’t even have a written review. I’m not a fan of this and will push myself some in 2019 to do a better job.
2018 Goal
There are a few more books on writing that I’ve wanted to read for a while. I’m planning on reading at least one of them this year. I’m also want to read more Octavia Butler. - Me (in the previous reading post)
That was my goal for 2018. It breaks down into two goals:
Read at least one book on writing.
Read more Octavia Butler.
I succeeded on the Octavia Butler goal and completely failed with the other.
2018 Numbers
I read 43 books for a total of 16,213 pages. This is a bit less than last year but still a fair amount.
Highlights
Below is a list of my five star books from 2018. The book titles link to Amazon and are affiliate links. The other links are to my Goodreads review. Unfortunately, this year I didn’t do a great job of always writing a review so some of them are missing or very short.
I generally highlight a lot of passages while reading and then rarely go back to look at them. I’ve included links to my highlights. Are they worthwhile without the context of the book? I have no idea. I’ve reread them and got something out of them but many are also so far removed from my memory that they are almost useless.
Being Mortal: Medicine and What Matters in the End by Atul Gawande
This book deals with the end of our lives. It was great. There is a lot of good insight here. Like a lot of the non-fiction books I read, I really should go back and take notes on what I highlighted.
We’re all going to deal with death and sickness. This book can help.
Sapiens: A Brief History of Humankind by Yuval Noah Harari
My entire Goodreads review is two sentences.
This is an incredible book. You should read this. - Me
I still agree with this. My friend, Steve Deobald, described this book as “the most lucid book he’s ever read.” There is a reason this book has a 4.45 rating on Goodreads. Go read the blurb about it there and then buy and read this book1.
Essentialism: The Disciplined Pursuit of Less by Greg McKeown
If you don’t prioritize your life, someone else will. - Greg McKeown
A really great book encouraging you to focus on what matters and, as a result, make a bigger impact and be happier. It is better to make a mile of progress on one thing instead of making inches of progress in a bunch.
Tim Ferris recently published a podcast with Greg McKeown which I’d also recommend. I’ve enjoyed listening to the podcast after a bit of time away from the book. This has helped reinforce ideas from the book. If you’re hesitant to read the book, take the time to listen and pay attention to this long podcast.
I highlighted over 100 sections of this book. I plan on revisiting these notes and this book periodically.
Crucial Conversations Tools for Talking When Stakes Are High by Kerry Patterson, Joseph Grenny, Ron McMillan, Al Switzler
A crucial conversation is one where the stakes are high, opinions vary, and emotions run strong. This book provides guidance for handling those conversations better.
I enjoyed this book and thought I picked up some useful tips from it. I think this is another where doing follow up work would help solidify some of the concepts.
Rediscovering JavaScript: Master ES6, ES7, and ES8 by Venkat Subramaniam
Do you write JavaScript?
Did you write JavaScript in the past but then move on to languages like ClojureScript and miss all the changes that happened to JavaScript?
Both of those sentences apply to me. This book has been great at catching up on modern JavaScript. I find myself referencing it while writing JavaScript and it has been very helpful. It is to the point and I find myself referencing it periodically.
CivilWarLand in Bad Decline by George Saunders
I really like this book. It is a wonderful collection of short stories. This was my second time reading it and I still enjoyed it.
The Obelisk Gate (The Broken Earth #2) and The Stone Sky (The Broken Earth, #3) by N.K. Jemisin
N.K. Jemisin has won a Hugo three years in a row. Those three years line up with each release of a book in The Broken Earth series. They are really good.
This series is great. The world is interesting and the story compelling. I highly recommend it.
The Hate U Give by Angie Thomas
Reading lets you experience life from a different perspective. This book is good. It was quickly made into a movie which is also pretty good.
I read this as part of my book club and it was universally enjoyed.
Six of Crows (Six of Crows, #1) and Crooked Kingdom (Six of Crows #2) by Leigh Bardugo
I just really enjoyed this series. I enjoyed the fantasy world it was set in and have read most of Leigh Bardugo’s other books that are set in this same world.
The series is a young adult series. It isn’t complex. The reading isn’t difficult. It isn’t going to change your life and you’re not going to be blown away by the writing. It almost feels weird to include this series in the same list as CivilWarLand and The Broken Earth series. Even still, I found myself sucked into the story and didn’t mind spending the short amount of time it took to read the books.
Non-Five Star highlights
Life 3.0: Being Human in the Age of Artificial Intelligence by Max Tegmark
I really enjoyed this. Pink references other works to build a narrative about how timing matters. When should you take a nap? Is it better to go do the doctors in the morning or afternoon? How do are cognitive abilities generally change throughout the day? How should you try to end your vacations?
I did take some notes on the book while reading it and I have referenced them. It was a good book. I should have taken more notes.
This is a great collection of short stories and non-fiction articles written by Octavia Butler. I really love her writing. I’ve read a few of her works and still enjoy Lilith’s Brood the most.
Below is a quote from her about science fiction that really resonated with me. It really hits home on one of the reasons I love reading science fiction.
But still I’m asked, what good is science fiction to Black people? What good is any form of literature to Black people? What good is science fiction’s thinking about the present, the future, and the past? What good is its tendency to warn or to consider alternative ways of thinking and doing? What good is its examination of the possible effects of science and technology, or social organization and political direction? At its best, science fiction stimulates imagination and creativity. It gets reader and writer off the beaten track, off the narrow, narrow footpath of what “everyone” is saying, doing, thinking—whoever “everyone” happens to be this year. And what good is all this to Black people? - Octavia Butler
This book is great. It is very approachable and dispels some wrong common knowledge.
Stats
I struggled generating stats this year. I kept having data issues with Goodreads. There is data that is in Goodreads that is failing to export both through their export feature and API. I’m somewhat wondering what I would need to do to track reading in a different way.
Below is the reading stats per month. The numbers are based on when the book is completed. December is partially so low because the other books all carried over to January.
Electronic books continue to make up the majority of the books I’m reading.
There are two physical books not included in my read books that I started and still need to finish. They are a both books focused on fitness (climbing injuries and proper movement) and aren’t books I’m actively reading.
Nearly a third of my reading was non-fiction. For the second year in a row, only two of those were software related.
1234
| | Number of books |
|-------------+-----------------|
| fiction | 29 |
| non-fiction | 14 |
2019 Goals
I have a stack of software and process books and I’d like to read through at least some of them (others are more reference books). I’m also going to bring over the 2018 goal of reading at least one book on writing.
In a more general sense, I’m hoping to put some practices together that help me gain more from the books I’m reading. I’m still thinking through what that means.
In the beginning of 2019 I also read Harari’s “21 lessons for the 21st Century.” Spoiler alert: this book will end up in my 2019 reading summary post.↩
I’ve been using Emacs in a remote tmux session lately and I’ve been missing lein-test-refresh notifications when my Clojure tests pass or fail. Luckily, it only took me a little bit of searching to figure out a solution for when I’m working inside of tmux.
Below is a GIF of the notifications I get as my tests run and pass or fail.
With the above notifications, I can keep my focus on my code and only switch to the tmux window with lein test-refresh running when a test fails.
This was pretty easy to setup. You can trigger a message in tmux by running tmux display-message <MESSAGE_HERE>. To configure lein-test-refresh to send notifications to tmux simply include the following in your :test-refresh section of your project.clj or profiles.clj.
In an older article of mine I extolled the virtues of having unified interfaces for interacting with your projects. I recently started working at Coinbase and the group I’m working with is mostly using makefiles as that common interface. We still have some more work to do to unify the makefile targets of the various projects but I’ve picked up one tip that makes switching between projects easier.
That tip is to have the default target of your makefile be one that prints out a helpful message. This looks like the following.
There is a lot going on there but it basically looks through your makefile targets and finds the ones that have a comment starting with ## after the target dependencies. Those targets are printed to the console along with the comment.
As an example, the makefile for my website looks similar to the below file.
123456789101112131415161718192021222324252627
.PHONEY: help
help:
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
sort | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONEY: watch
watch: ## Watch for changes and serve preview of site with drafts
bundle exec rake clean
bundle exec rake preview
.PHONEY: develop
develop: ## Serve a preview of the site without drafts and refresh changes
bundle exec rake clean
bundle exec rake develop
.PHONEY: new_adventure
new_adventure: ## Start a new adventure post
bundle exec rake new_adventure
.PHONEY: new_post
new_post: ## Start a new post
bundle exec rake new_post
.PHONEY: deploy
deploy: ## deploy
./deploy.sh
When this file, when I run make in this websites source, I get the following output.
1234567
0 [last: 0s] 21:11:50 ~/src/jakemcc/blog (master)
$ make
deploy deploy
develop Serve a preview of the site without drafts and refresh changes
new_adventure Start a new adventure post
new_post Start a new post
watch Watch for changes and serve preview of site with drafts
This is super useful when you’re starting doing work in a new project. With this feature you can get a quick list of useful targets and a description. It allows you to quickly see what can be done in a project.
If you follow my work you probably know that I value fast feedback cycles. Most of the open-source I maintain was developed to enable faster feedback cycles. This is why lein-test-refresh and lein-autoexpect were originally created.
Leiningen supports test selectors and lein-test-refresh does as well. This lets you start-up a testing session and only run tests or namespaces with certain metadata on them. This is a super useful feature as it lets you narrow your testing scope to one (or a handful) of tests while working on solving a specific problem.
lein-test-refresh now has built-in functionality that allows you to focus your test scope without restarting the Leiningen test process. If lein-test-refresh sees a deftest or ns form marked with :test-refresh/focus true in its metadata, then it will only run tests marked with :test-refresh/focus.
I was recently having some difficulty debugging a problem in a
ClojureScript single page application. The SPA was implemented using
reagent1.
This interface stores most of its state in a global
reagent.core/atom called db. To debug the problem, I thought it
would be useful to track how the global state changed as I interacted
with the interface. How do we do that?
For the rest of this article, pretend that (require '[reagent.core
:as reagent]) has been executed.
First, let’s define db-history in the same namespace as the global
reagent/atom, db. This is where we’ll collect the changes to db.
Next, let’s write a function called aggregate-state. This function
grabs the current value in db and conjs it onto db-history. It
also limits the history to the most recent 101 states.
Now we need to invoke aggregate-state whenever db changes. We can
do this using reagent/track. reagent/track takes a function and
optional arguments and invokes that function whenever a reagent/atom
that function depends on changes.
reagent/track! is similar except it immediately invokes the function
instead of waiting for the first change. We can use it to cause
aggregate-state to get called whenever db changes.
Now history of the global state is being tracked. But we need a way to
access it. Below is what I ended up writing. When you call
ui.data.history() in Chrome’s JavaScript console, it returns an
object you can click on to explore. If you pass in strings as
arguments to history then it only selects some of the data from the
global db and history.
It only took about fifteen lines of code to gain a view of our
application’s state changes over time. This view helped me solve my
problem. Hopefully it will help you too.
This particular project is nearly four years old and has had many hands on it over the years. Working in it reminds me of how useful re-frame is on larger applications like this one.↩
A couple months ago I was looking into a problem and noticed that
there was a situation where an expensive operation could be running
simultaneously multiple times. This was wasteful.
This operation happened on a timer and could also be triggered by a power
user through the UI. A power user could accidentally (or purposefully)
mash on a UI button and cause the instance they’re interacting with
to grind to a halt1.
It was pretty easy to prevent. All I needed to introduce was an
atom and lean on compare-and-set!. compare-and-set! is a pretty
neat function (and concept found in many languages). Here is the docstring:
Atomically sets the value of atom to newval if and only if the
current value of the atom is identical to oldval. Returns true if
set happened, else false
Basically, compare-and-set! changes the value of an atom only if it
starts from a specified value and returns a boolean letting you know
if it did.
To prevent an operation from running multiple times, introduce an atom
and wrap calling the operation in a conditional using
compare-and-set!. After doing the work, be sure to reset! your
atom back to the starting value.
Below is the code.
123456789101112
(defonce running?(atomfalse))(defn- expensive-operation!'[];; do work)(defn expensive-operation(try(expensive-operation!')(finally(reset!running?false)))))
OK, not really grind to a halt, but consume unnecessary resources.↩
I typically like to reflect on my previous years reading closer to the beginning of the next year. We are just entering March, so I’ve missed doing that.
Here are links to my previous end-of-year reflections: 2013, 2014, 2015, 2016.
I’ve continued to keep track of my reading using Goodreads. My profile continues to have the full list of the books I’ve read since 2010. Here is my entire 2017 record.
2017 Goal
My goal entering 2017 was to revisit some past favorites. I started this goal without setting a number, so I’ll just have to trust how I feel about it.
In 2017, I reread Frank Herbert’s Dune and John William’s Stoner. I also read new-to-me books by the authors David Foster Wallace, Haruki Murakami, George Saunders, and Neal Stephenson. I’ve also reread a George Saunders book in the first part of 2018.
I mostly achieved 2017’s goal. If I had reread another book, I’d consider it 100% completed, but I’m going to count reading some favorite authors towards the goal.
2017 Numbers
I read a total of 49 books for a total of 17,853 pages. I also read every issue of Amazon’s Day One weekly periodical1.
The number of five-star books I read this last year was low compared to previous years.
Recommendations
I only gave out seven five-star ratings. Two of the seven were books I reread. Title links are affiliate links to Amazon and the review links are to my review on Goodreads.
Below are more details on some of the above five-star books and some shoutouts for some non-five star books. Looking back over my books, I’d recommend any four-star or higher book without hesitation but am not going to put them all here.
Lilith’s Brood by Octavia Butler
Lilith’s Brood was one of the last books I read in 2017. It is a three book series published as a single book. It is amazing. This series achieves precisely what I want in a great science fiction book. I highly recommend this book. Reading this book reminded me why I love reading.
A quote from a non-fiction essay by Octavia Butler describes why good science fiction is fantastic.
But still I’m asked, what good is science fiction to Black people? What good is any form of literature to Black people? What good is science fiction’s thinking about the present, the future, and the past? What good is its tendency to warn or to consider alternative ways of thinking and doing? What good is its examination of the possible effects of science and technology, or social organization and political direction? At its best, science fiction stimulates imagination and creativity. It gets reader and writer off the beaten track, off the narrow, narrow footpath of what “everyone” is saying, doing, thinking—whoever “everyone” happens to be this year. And what good is all this to Black people? - Octavia Butler
The Sense of Style by Steven Pinker
Yes, I read a book on writing and think this is one of the top books I read last year. I initially read a Kindle edition from my local library and then immediately bought the hardcover so I can reference it while writing.
The writing is great. The book is humorous. I’d highly recommend to anyone that writes. I should reread this.
Dune by Frank Herbert
Dune is a classic for a reason. It was still great my second time through it. If you haven’t read Dune, you are missing out.
If you read it on a Kindle, I have a custom Kindle dictionary that makes reading it more pleasurable.
Stoner by John Williams
It is still unclear to me why I like this book so much, but I do. The writing is crisp. The story is depressing.
Stories of Your Life and Others by Ted Chiang
Over the years I’ve started to enjoy reading short story collections. Every story in this collection was great. I devoured this book and then everything else I could find by Ted Chiang.
Capital in the Twenty-First Century by Thomas Piketty
This is a massive book. It probably deserved five-stars. It presents a ton of information to the reader. It is boring. It also made me think about the role of taxes in society and changed my thoughts about them.
If you’ve been putting this off, you can probably skip to the last section and still get a lot from this book.
Here is a review that does a spot on job of describing the book. Here is an Amazon link and my own review.
Bobiverse Series by Dennis Taylor
This is a fun light-hearted science fiction series. It still manages to explore some deep topics. Read the description and if it sounds interesting to you, pick it up.
Stats
Similar to last year, April and September were times when I wasn’t reading a ton.
This year physical books made a comeback. I checked out more physical books from the library this year than in the past.
I read a lot of non-fiction books this year. Only two of them were directly related to software.
1234
| | Number of books |
|-------------+-----------------|
| fiction | 30 |
| non-fiction | 19 |
2018 Goals
There are a few more books on writing that I’ve wanted to read for a while. I’m planning on reading at least one of them this year. I’m also want to read more Octavia Butler.
Unfortunately, this periodical has ended after years of publishing once a week. I’m bummed. I really enjoyed receiving a short story and poem once a week.↩