Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate slow SQL database in test?

I have a bug that manifest himself only when database is slow. It applies to even simplest database operations (select, etc).

I would like to create test, where I force my database to be slow. How to do that?
I use Spring Boot, Spring Data, Hibernate, MariaDB.

Ideally, I want the 'slow database' part to be completely contained in the test code, in my java application. That way, test will be completely automated and self-contained.

I want to slow down database access only for one test (not globally, for all access).

I was proposed to introduce database trigger (BEFORE SELECT) with sleep

But this is not flexible, because it slows down every access, not access just for one test.

like image 699
Bartosz Bilicki Avatar asked Sep 07 '16 12:09

Bartosz Bilicki


1 Answers

I see four possible solutions for this problem.

  1. You don't have to create slow database, you can create slow connection to the database. If you run database on a different (Virtual) machine, there are systems that help simulating shitty internet connections by delaying network responses randomly.

  2. You can use sleep(10) function that is provided by your database, this would require "injecting" it into SQL query or override method for the purpose of test and replace SELECT with SELECT SLEEP(10).

  3. Simulate stress-test on the database with mysqlslap if you use mysql.

  4. Another solution, a bit stupid tho, you can use spring-aop and attach a delay aspect before and after the DAO method execution with random small sleep. This way you have control over it, don't have to modify existing code and let spring make the job of doing the delay without integration into real-system. Not that stupid after all. This one is quite flexible and I think I would go with it. Easiest to setup.

If it's stupid, but it works, it's not stupid.

like image 155
agilob Avatar answered Oct 02 '22 01:10

agilob