I think I’m a good programmer. If it’s Python again, I’m the best at it! However, I this “top master” was his own pit, but also another master to fool.

It all starts with a Singleton.

Python Singleton is easy, but

class Singleton:
    ob = None
    
    def __new__(cls):
        if cls.ob is None:
            cls.ob = SomeThing
        return cls.ob
Copy the code

Instead, I wrote

class Singleton:
    connetion = None
    
    def __new__(cls):
        if cls.connection is None:
            return NewConnection(PoolSize=xxx)
        return cls.connection
Copy the code

Do you see the problem? This is not reuse at all ah!!

This code is database connection code, causing each database request to open a new connection. I get hundreds of requests a minute. The database connection explodes.

Colleague in code review: “You don’t seem right?”

Me: Python Singleton is like this.

Colleague: Are you sure?

Me: I’m sure, I’m Py master.

Colleague: It does seem to be wrong.

Me: No, no, no, you worry too much.

Colleague: Ok, go online.

What a systematic liar I am!

If this is retarded, then there are more retarded.

I actually have tests that show the last line of this file

        return cls.ob
Copy the code

It never ran. But because the file is small, there is 93% coverage, I think nothing, just ignore!

This file contains only one class method. All the others are import, 93% and 50% are the same meaning. Also, the entire backend depends on the code, obviously 100%.

I ignored it for days until the database exploded, and it took 3 hours!

I checked all kinds of thread safety (we used coroutine, there were no threads at all), concurrency safety, memory issues…

MDZZ!


Of course, these unhelpful checks showed me room for optimization at several deployment levels.

How to avoid

Set minimum test coverage for different file modules. For example, critical code must be 100%, middleware 95%, user-layer 90%, and so on. The testing requirements of different layers of code are different. Even if all tests are passed, a test coverage failure is considered a failure.

Also, be sure not to manually check coverage, but to automate it with tools that become part of CI.