Simmy Chaos Engine for .NET – Part 4, Doing Some Real Damage, Dropping a Table

Full source code here.

Want to learn more about Polly? Check out my Pluralsight course on it.

Up until now the Simmy examples I’ve written have thrown exceptions, changed successes to failures or slowed down a request. None of these did any real damage to your system, and your application would probably have recovered when the next request came along.

But what about dropping a table? Poof, all the data is gone. Now what does your application do?

Doing this is very easy with the Behavior clause. Like the other clauses it takes a probability, an enabled flag, and action or func that executes any piece of code.

The Scenario I have Web API application with a products controller that queries the database for a list of products.

During application start up the database is created and populated in Configure method of Startup.cs using the EnsureCreated() method and custom seed class.

Inside the controller the action method uses the SalesContext to query the database.

Inside the controller’s constructor I have the chaos policy, it is set to drop the Products table. The drop action of the behavior policy will execute 25% of the time the policy is run. If get an error when you run this saying that the table is absent, it means the chaos policy dropped the table, you’ll have to restart the application to run through the database seeding and creation again.

The policy looks like this -

 1public ProductsController(SalesContext salesContext)
 2{
 3	_behaviorPolicy = MonkeyPolicy.InjectBehaviourAsync(
 4		 async () =>
 5		 {
 6			 await _salesContext.Database.ExecuteSqlCommandAsync("DROP TABLE Products");
 7			 Console.WriteLine("Dropped the Products table.");
 8		 },
 9		 0.25, // 25% of the time this policy will execute
10		async () => await Task.FromResult(true));
11	_salesContext = salesContext;
12}

The request to the _salesContext is made inside the policy, but the policy executes before the call the db is made and in 25% of such calls the table will be dropped.

1[HttpGet]
2public async Task<ActionResult> Get()
3{
4	List<Product> products =  await _behaviorPolicy.ExecuteAsync(async () => await _salesContext.Products.Take(10).ToListAsync());
5
6	return Ok(products);
7}

You can of course execute and code from inside the policy, wipe a cache, kill a service, delete a file, etc.

This example might be a little excessive in the damage it does, you can decide if it is unreasonable to expect you application to continue working even when the database is unavailable.

Full source code here.

comments powered by Disqus

Related