如何使用JUnit测试读写锁?

huangapple go评论62阅读模式
英文:

How to test read-write lock using JUnit?

问题

我有两个方法,一个用于读取,一个用于写入。它们都访问同一集合,并且在多个线程之间使用。例如,三个线程用于写入,八个线程用于读取。我需要测试集合的线程安全性。写入方法具有优先权。

public static void insertEntity(Entity entity) {
    try {
        readWriteLock.writeLock().lock();

        Session session = sf.openSession();
        session.beginTransaction();

        GenericDAO.insertEntity(session, entity);

        session.getTransaction().commit();
        session.close();
    } finally {
        readWriteLock.writeLock().unlock();
    }
}

public static Entity getByID(Integer id) {
    try {
        readWriteLock.readLock().lock();

        Session session = sf.openSession();
        Entity entity = GenericDAO.selectByID(session, Entity.class, id);

        session.close();
        return entity;
    } finally {
        readWriteLock.readLock().unlock();
    }
}
英文:

I have two methods, one for reading and one for writing. They both access the same collection and are used across multiple threads. For example, three threads for writing and eight - for reading. I need to test the thread safety of my collection. The write method takes precedence.

public static void insertEntity(Entity entity) {
        try {
            readWriteLock.writeLock().lock();

            Session session = sf.openSession();
            session.beginTransaction();

            GenericDAO.insertEntity(session, entity);

            session.getTransaction().commit();
            session.close();
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }


public static Entity getByID(Integer id) {
        try {
            readWriteLock.readLock().lock();

            Session session = sf.openSession();
            Entity entity = GenericDAO.selectByID(session, Entity.class, id);

            session.close();
            return entity;
        } finally {
            readWriteLock.readLock().unlock();
        }

    }

答案1

得分: 1

如果你想测试你的锁处理逻辑,你应该将那部分逻辑提取到自己的类中。
在持有锁的同时需要执行的操作应该封装到一个接口中。
你的生产代码实现将处理实体逻辑。你的测试代码实现将强调锁处理(例如,断言互斥性)。

顺便说一下:你的锁定代码是不正确的。你应该只在对应的lock()成功的情况下才执行unlock()。模式如下:

lock.lock();
try {
    // 做一些操作
} finally {
    lock.unlock();
}
英文:

If you want to test your lock handling, you should extract that logic in to its own class.
What has to be done while holding the lock should be encapsulated into an interface.
Your production code implementation will do the Entity stuff. Your test code implementation will stress the lock handling (e.g. asserting mutual exclusivity)

By the way: your locking code is not correct. You should unlock() a lock only if the corresponding lock() succeeded. The pattern goes like this:

lock.lock();
try {
    // do stuff
} finally {
    lock.unlock();
}

huangapple
  • 本文由 发表于 2020年10月21日 15:17:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64458472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定