Exercise: When the Weather Service Is Down
Take Maya's ticket after a bad morning: the hub's briefing gives up on one missed call, keeps knocking on a weather service that's down for an hour, and crashes on a bedroom with no speaker. Make it try again politely, leave the service alone while it's down, and cope with the missing speaker, with the tests green throughout and a fake clock so no test ever waits.
Freelancer · C# · Java · Python · JavaScript · TypeScript · Updated
Last Tuesday, SkyCast was down for an hour, and HomeBase had a bad morning. Maya has written it up as a ticket. The hub's morning briefing works on a good day; your job is to make it hold up on a bad one, without breaking anything that works today.
Before you start
You need one language set up, as in Setup: VS Code and Your Language, and to know how to run the tests, as in Running the Exercise Tests. The exercise is written in all five of the course's languages; pick the one you use.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-dp-weather-outage, also linked from the Exercise box beside the lesson. Clone it into your course folder. In a terminal, in that folder:
git clone https://github.com/ask-codey/exercise-dp-weather-outage.git
Git makes a folder, exercise-dp-weather-outage, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the hub's code, as it is today: the morning briefing, the rooms, and the hub's promises for a speaker, SkyCast and the clock (the repository's
README.mdnames the files for each language); - the fakes: stand-ins for SkyCast, the clock and a speaker. The fake SkyCast answers from a script of "sunny" and "down" and counts its calls, and the fake clock writes down every wait instead of waiting, so a test of a 30-second outage runs in a blink;
- the tests, which you don't need to change;
- a demo program that runs a week of mornings at Maya's house.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: Maya, who owns the house (and the HomeBase hub)
- One missed call shouldn't cost the morning its weather. Try SkyCast up to 3 times, waiting 1 second before the second try and 2 seconds before the third, then give up and show
Weather: unavailable, as today. Wait with the hub's clock, never a real pause.- When SkyCast is really down, leave it alone for a while. After 3 briefings in a row that couldn't get the weather, don't call SkyCast at all for 30 seconds: show
Weather: unavailablestraight away. After 30 seconds, let the next briefing try again. If that works, carry on as normal; if it doesn't, leave SkyCast alone for another 30 seconds. A briefing that gets the weather starts the count again.- The new bedroom has no speaker yet, and the briefing crashed on it. Other teams' routines call
room.speakeron every room and won't be adding checks, so a room with no speaker needs a speaker that's safe to call and does nothing.Everything that works today has to keep working.
The repository's README.md has the ticket in full. The hub already hands the briefing its clock, with now() and sleep(seconds), but the briefing doesn't use it yet.
What to do
- Run the tests first. Three pass: they're what the hub does today. Eight fail: they're the ticket.
- Read the code and find the smells. What does the briefing do with a failure? What does it remember about the last few mornings? What is a room's speaker when it has none?
- Change the code in small steps, and run the tests after every step. The three that pass must stay green the whole time.
- Stop when all eleven are green and the demo prints what When you're done shows.
The README's Stuck? section names the patterns, if you want them. Try a few minutes without it first.
Check your work
Each language has its own command, run in the terminal in your language's folder (Terminal > New Terminal in VS Code). The repository's README.md has the full list, including the one-time setup for Python (a virtual environment and pytest) and TypeScript (npm install).
| Language | Run the tests | Run the demo |
|---|---|---|
| C# | dotnet test |
dotnet run --project WeatherOutage |
| Java | ./mvnw test (.\mvnw test in PowerShell) |
java src/main/java/Demo.java |
| Python | python -m pytest |
python demo.py |
| JavaScript | npm test |
node demo.js |
| TypeScript | npm test |
node demo.ts |
A failing test's message starts with Not yet: for the ticket's work, or Keep this working: if a change broke something that worked, and says what it expected, for example Not yet: after 3 failed briefings in a row, the 4th doesn't call SkyCast at all. The starter's demo stops with an error when it reaches the bedroom; that's part of the ticket.
Hints
- Do the three parts one at a time, in the ticket's order. Each has its own tests.
- Count with the fakes. The tests check two things you can't see on a screen: how many times SkyCast was called, and every wait the clock was asked for. A wait you take with a real pause won't show up, and will make the tests slow.
- The retry and the outage are two different jobs. One gives a hiccup a second chance; the other remembers that the last mornings failed. Try keeping them in two small pieces, with the retry inside.
- Only SkyCast's own "didn't answer" error is worth trying again.
- For the bedroom, look at what other teams' code does: it calls
room.speaker.stop()on every room, with no check. What would a room with no speaker have to hold for that to be safe?
When you're done
All 11 tests are green, and the demo prints the same eight lines in every language:
A hiccup: Weather: 72F, sunny (SkyCast calls: 2, waited 1 s)
SkyCast down: Weather: unavailable (SkyCast calls: 3, waited 3 s)
SkyCast down: Weather: unavailable (SkyCast calls: 3, waited 3 s)
SkyCast down: Weather: unavailable (SkyCast calls: 3, waited 3 s)
SkyCast down: Weather: unavailable (SkyCast calls: 0, waited 0 s)
30 s later: Weather: 72F, sunny (SkyCast calls: 1, waited 0 s)
With the bedroom: Weather: 72F, sunny (SkyCast calls: 1, waited 0 s)
The news played 7 times in the kitchen
Read it as a story. The hiccup costs one extra call and a 1-second wait instead of the morning's weather. Three failed briefings each try three times; the fourth answers at once without bothering SkyCast. Thirty seconds later, one briefing tries again, and SkyCast is back. And the bedroom, with no speaker, doesn't stop anything.
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-dp-weather-outage), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-dp-weather-outage.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-dp-weather-outage, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
The hub now has a bad day the way good software does: a hiccup costs a second, an outage costs nothing but an honest "unavailable", and a missing speaker costs nothing at all. None of it needed a real outage, or a real minute of waiting, to prove.