MkCounter.java returns timestamps instead of counter values

未關閉 適合新手
#151 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
1/5
預估耗時
1 小時以內
新手友好度
88/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
java
領域
testing

研究方向

開啟 src/main/java/co/stateful/mock/MkCounter.java,查看第 29–36 行,其中 set 為空,incrementAndGet 會回傳 System.currentTimeMillis()。依照 issue 中的程式碼片段所示,新增一個 AtomicLong 欄位,並將這兩個方法都委派給它。完成的標準是:counter.set(10L) 後接 counter.incrementAndGet(-3L) 會回傳 7,且正、零和負增量在同一實例上的行為都正確;執行 src/test/java 下與 mock 相關的測試來確認。

由索引模型根據 Issue 內容生成。

描述

MkCounter never stores a counter value: set() discards its argument, and incrementAndGet() ignores the delta and returns the wall-clock time. Code using the mock therefore gets a timestamp instead of the counter result, even though the mock package documents using MkSttc as a normal adapter in unit tests.

In src/main/java/co/stateful/mock/MkCounter.java, lines 29–36:

public void set(final long value) {
    // done
}

public long incrementAndGet(final long delta) {
    return System.currentTimeMillis();
}

For example, on a single counter instance:

Counter counter = new MkSttc().counters().get("hits");
counter.set(10L);
long result = counter.incrementAndGet(-3L);
// Expected: 7L. Actual: current epoch time in milliseconds.

Keep the value in an AtomicLong field and delegate both operations to it:

private final AtomicLong value = new AtomicLong();

public void set(final long number) {
    this.value.set(number);
}

public long incrementAndGet(final long delta) {
    return this.value.addAndGet(delta);
}

This confines the fix to MkCounter and makes positive, zero, and negative deltas follow the Counter contract on the same instance.

主要語言
Java
星號
10
分支
3
平均合併
6 小時 5 分鐘
30 天內合併 PR
10

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

sttc/java-sdk 的其他 Issue

查看 sttc/java-sdk 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。