IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    设计模式之美-课程笔记19-规范与重构3

    10k发表于 2023-07-11 00:00:00
    love 0

    理论三:什么是代码的可测试性?如何写出可测试性好的代码?

    • 什么是代码的可测试性
    • 如何写出可测试的代码
    • 有哪些常见的不好测试的代码

    实战案例

    Transaction 是经过抽象简化之后的一个电商系统的交易类,用来记录每笔订单交易的情况。Transaction 类中的 execute() 函数负责执行转账操作,将钱从买家的钱包转到卖家的钱包中。真正的转账操作是通过调用 WalletRpcService RPC 服务来完成的。除此之外,代码中还涉及一个分布式锁 DistributedLock 单例类,用来避免 Transaction 并发执行,导致用户的钱被重复转出。

    public class Transaction {
      private String id;
      private Long buyerId;
      private Long sellerId;
      private Long productId;
      private String orderId;
      private Long createTimestamp;
      private Double amount;
      private STATUS status;
      private String walletTransactionId;
      
      // ...get() methods...
      
      public Transaction(String preAssignedId, Long buyerId, Long sellerId, Long productId, String orderId) {
        if (preAssignedId != null && !preAssignedId.isEmpty()) {
          this.id = preAssignedId;
        } else {
          this.id = IdGenerator.generateTransactionId();
        }
        if (!this.id.startWith("t_")) {
          this.id = "t_" + preAssignedId;
        }
        this.buyerId = buyerId;
        this.sellerId = sellerId;
        this.productId = productId;
        this.orderId = orderId;
        this.status = STATUS.TO_BE_EXECUTD;
        this.createTimestamp = System.currentTimestamp();
      }
      
      public boolean execute() throws InvalidTransactionException {
        if ((buyerId == null || (sellerId == null || amount < 0.0) {
          throw new InvalidTransactionException(...);
        }
        if (status == STATUS.EXECUTED) return true;
        boolean isLocked = false;
        try {
          isLocked = RedisDistributedLock.getSingletonIntance().lockTransction(id);
          if (!isLocked) {
            return false; // 锁定未成功,返回false,job兜底执行
          }
          if (status == STATUS.EXECUTED) return true; // double check
          long executionInvokedTimestamp = System.currentTimestamp();
          if (executionInvokedTimestamp - createdTimestap > 14days) {
            this.status = STATUS.EXPIRED;
            return false;
          }
          WalletRpcService walletRpcService = new WalletRpcService();
          String walletTransactionId = walletRpcService.moveMoney(id, buyerId, sellerId, amount);
          if (walletTransactionId != null) {
            this.walletTransactionId = walletTransactionId;
            this.status = STATUS.EXECUTED;
            return true;
          } else {
            this.status = STATUS.FAILED;
            return false;
          }
        } finally {
          if (isLocked) {
           RedisDistributedLock.getSingletonIntance().unlockTransction(id);
          }
        }
      }
    }
    

    对于这段代码,如果要写单测,我们可以写出如下测试点:

    • buyer id 或sellerId设为空或者amount设置为负数;
    • 当Status是executed的时候,直接返回true;
    • 锁定未成功,返回false;
    • 锁定成功,当前时间和执行发起时间间隔超过14天,返回错误,状态置为过期;
    • 锁定成功,远程调用成功,正常返回执行状态,返回true
    • 远程调用失败,状态置为失败, 返回false
    • 交易已经执行,不在重复执行转钱逻辑,返回true

    我们先写一个成功转账的case,这个case主要依赖的服务是锁服务和远程调用。这里我们必须要实现或者构造一个请求调用这些才能让测试代码工作。开销大,实现复杂。

    public void testExecute() {
      Long buyerId = 123L;
      Long sellerId = 234L;
      Long productId = 345L;
      Long orderId = 456L;
      Transction transaction = new Transaction(null, buyerId, sellerId, productId, orderId);
      boolean executedResult = transaction.execute();
      assertTrue(executedResult);
    }
    

    单测是在测试我们自己写的代码,这些依赖的服务我们不需要也做不到去执行他们,为了测试我们的代码。所以有一个东西叫mock,可以让我们的测试与外部系统解依赖。

    所谓的 mock 就是用一个“假”的服务替换真正的服务。mock 的服务完全在我们的控制之下,模拟输出我们想要的数据。

    我们可以通过手动或者框架mock。这里简单用手动mock来让外部服务返回我们需要的数据,比如RPC不需要真正的网络调用。

    public class MockWalletRpcServiceOne extends WalletRpcService {
      public String moveMoney(Long id, Long fromUserId, Long toUserId, Double amount) {
        return "123bac";
      } 
    }
    
    public class MockWalletRpcServiceTwo extends WalletRpcService {
      public String moveMoney(Long id, Long fromUserId, Long toUserId, Double amount) {
        return null;
      } 
    }
    

    有趣的事情来了,原来的execute方法中的WalletRpcService是new出来的,可测试性差, 我们可以对原来的代码做一些重构。

    依赖注入是实现代码的可测试性最有效的手段。我们可以通过依赖注入,将这个Service的创建交给上层逻辑,再注入到当前的类中。

    public class Transaction {
      //...
      // 添加一个成员变量及其set方法
      private WalletRpcService walletRpcService;
      
      public void setWalletRpcService(WalletRpcService walletRpcService) {
        this.walletRpcService = walletRpcService;
      }
      // ...
      public boolean execute() {
        // ...
        // 删除下面这一行代码
        // WalletRpcService walletRpcService = new WalletRpcService();
        // ...
      }
    }
    

    这样一来我们的测试代码可以写成,不需要真正外部服务调用和实现。

    public void testExecute() {
      Long buyerId = 123L;
      Long sellerId = 234L;
      Long productId = 345L;
      Long orderId = 456L;
      Transction transaction = new Transaction(null, buyerId, sellerId, productId, orderId);
      // 使用mock对象来替代真正的RPC服务
      transaction.setWalletRpcService(new MockWalletRpcServiceOne()):
      boolean executedResult = transaction.execute();
      assertTrue(executedResult);
      assertEquals(STATUS.EXECUTED, transaction.getStatus());
    }
    

    对于RedisDistributedLock, 他是一个单例,我们无法mock(重写方法和继承)也不能依赖注入来替换。

    如果他是我们维护的,我们可以重构他,但是如果不是,我们就需要将原来的execute实现代码做一些修改是他更好测。

    public class TransactionLock {
      public boolean lock(String id) {
        return RedisDistributedLock.getSingletonIntance().lockTransction(id);
      }
      
      public void unlock() {
        RedisDistributedLock.getSingletonIntance().unlockTransction(id);
      }
    }
    
    public class Transaction {
      //...
      private TransactionLock lock;
      
      public void setTransactionLock(TransactionLock lock) {
        this.lock = lock;
      }
     
      public boolean execute() {
        //...
        try {
          isLocked = lock.lock();
          //...
        } finally {
          if (isLocked) {
            lock.unlock();
          }
        }
        //...
      }
    }
    

    给上锁的逻辑包起来,然后在单测中去mock这个函数。

    public void testExecute() {
      Long buyerId = 123L;
      Long sellerId = 234L;
      Long productId = 345L;
      Long orderId = 456L;
      
      TransactionLock mockLock = new TransactionLock() {
        public boolean lock(String id) {
          return true;
        }
      
        public void unlock() {}
      };
      
      Transction transaction = new Transaction(null, buyerId, sellerId, productId, orderId);
      transaction.setWalletRpcService(new MockWalletRpcServiceOne());
      transaction.setTransactionLock(mockLock);
      boolean executedResult = transaction.execute();
      assertTrue(executedResult);
      assertEquals(STATUS.EXECUTED, transaction.getStatus());
    }
    
    1. 对于过期执行的测试,我们先写出测试代码:
    public void testExecute_with_TransactionIsExpired() {
      Long buyerId = 123L;
      Long sellerId = 234L;
      Long productId = 345L;
      Long orderId = 456L;
      Transction transaction = new Transaction(null, buyerId, sellerId, productId, orderId);
      transaction.setCreatedTimestamp(System.currentTimestamp() - 14days);
      boolean actualResult = transaction.execute();
      assertFalse(actualResult);
      assertEquals(STATUS.EXPIRED, transaction.getStatus());
    }
    

    这个代码的问题,我也不知道。直到看了作者的解释。

    这个代码暴露了set方法,setCreatedTimestamp这个违反了类的封装。原本这种值都是自动生成的,暴露他带来了灵活性,但是也带来了不安全与不稳定。(有人可能意外调用这个方法并且改变了这个值)

    1. 对于这种方法,包含跟时间有关的“未决行为”,一般处理方式是将这种未决行为重新封装。
    public class Transaction {
    
      protected boolean isExpired() {
        long executionInvokedTimestamp = System.currentTimestamp();
        return executionInvokedTimestamp - createdTimestamp > 14days;
      }
      
      public boolean execute() throws InvalidTransactionException {
        //...
          if (isExpired()) {
            this.status = STATUS.EXPIRED;
            return false;
          }
        //...
      }
    }
    
    1. 重构之后,我们的测试就比较好写了:
    public void testExecute_with_TransactionIsExpired() {
      Long buyerId = 123L;
      Long sellerId = 234L;
      Long productId = 345L;
      Long orderId = 456L;
      Transction transaction = new Transaction(null, buyerId, sellerId, productId, orderId) {
        protected boolean isExpired() {
          return true;
        }
      };
      boolean actualResult = transaction.execute();
      assertFalse(actualResult);
      assertEquals(STATUS.EXPIRED, transaction.getStatus());
    }
    
    1. Transaction类的构造函数还有点可重构的地方,就是他不止有赋值操作。我们可以将这部分抽离出来一个函数,单独测试。
    public Transaction(String preAssignedId, Long buyerId, Long sellerId, Long productId, String orderId) {
        //...
        fillTransactionId(preAssignId);
        //...
      }
      
      protected void fillTransactionId(String preAssignedId) {
        if (preAssignedId != null && !preAssignedId.isEmpty()) {
          this.id = preAssignedId;
        } else {
          this.id = IdGenerator.generateTransactionId();
        }
        if (!this.id.startWith("t_")) {
          this.id = "t_" + preAssignedId;
        }
      }
    

    其他的常见的Anti-Patterns

    1. 未决行为

    代码的输出是随机的或者说不确定的,比如跟时间或者随机数有关的。这种情况我们可以将相关的代码包起来以提升他的可测试性。例如:

    public class Demo {
      public long caculateDelayDays(Date dueTime) {
        long currentTimestamp = System.currentTimeMillis();
        if (dueTime.getTime() >= currentTimestamp) {
          return 0;
        }
        long delayTime = currentTimestamp - dueTime.getTime();
        long delayDays = delayTime / 86400;
        return delayDays;
      }
    }
    

    我们可以

    public class Demo {
      private boolean overDue(Date dueTime) {
          long currentTimestamp = System.currentTimeMillis();
          if (dueTime.getTime() >= currentTimestamp) {
            return 0;
          }
      }  
        
      public long caculateDelayDays(Date dueTime) {
        if (!overDue(dueTime)) {
            return 0;
        }
        long delayTime = currentTimestamp - dueTime.getTime();
        long delayDays = delayTime / 86400;
        return delayDays;
      }
    }
    

    在测试的时候就可以将ovderDue方法直接返回true或者false。

    2. 全局变量

    public class RangeLimiter {
      private static AtomicInteger position = new AtomicInteger(0);
      public static final int MAX_LIMIT = 5;
      public static final int MIN_LIMIT = -5;
    
      public boolean move(int delta) {
        int currentPos = position.addAndGet(delta);
        boolean betweenRange = (currentPos <= MAX_LIMIT) && (currentPos >= MIN_LIMIT);
        return betweenRange;
      }
    }
    
    public class RangeLimiterTest {
      public void testMove_betweenRange() {
        RangeLimiter rangeLimiter = new RangeLimiter();
        assertTrue(rangeLimiter.move(1));
        assertTrue(rangeLimiter.move(3));
        assertTrue(rangeLimiter.move(-5));
      }
    
      public void testMove_exceedRange() {
        RangeLimiter rangeLimiter = new RangeLimiter();
        assertFalse(rangeLimiter.move(6));
      }
    }
    
    1. 如果换了这些个测试用例顺序执行,第二个其实是会失败。因为第一个移动静态变量position到-1了。
    2. 所以可以在每次测试的时候将其值重置为0。但是有的测试框架是并发执行的,16、17、18、23可能回家交叉执行,影响到move的执行结果。

    3. 静态方法

    难mock。

    只有在这个静态方法执行耗时太长、依赖外部资源、逻辑复杂、行为未决等情况下,我们才需要在单元测试中 mock 这个静态方法。

    4. 复杂继承

    如果父类需要mock某个依赖对象,子类也都需要。如果继承关系很深或者很复杂,那么就会在编写子类测试的时候需要mock很多。

    所以要尽量使用组合而非继承保持类之间的关系相对扁平。

    5. 高耦合代码

    如果一个类职责很重,需要依赖十几个外部对象才能完成工作,代码高度耦合,那我们在编写单元测试的时候,可能需要 mock 这十几个依赖的对象。不管是从代码设计的角度来说,还是从编写单元测试的角度来说,这都是不合理的。



沪ICP备19023445号-2号
友情链接