Repository: go-kratos/kratos
Pull request #2146 (2022-06-30 09:52:54)

feat(contrib):add etcd watcher withKeysOnly option
<!--
🎉 Thanks for sending a pull request to Kratos! Here are some tips for you:

1. If this is your first time contributing to Kratos, please read our contribution guide: https://go-kratos.dev/en/docs/community/contribution/
2. Ensure you have added or ran the appropriate tests and lint for your PR, please use `make lint` and `make test` before filing your PR, use `make clean` to tidy your go mod.
3. If the PR is unfinished, you may need to mark it as a WIP(Work In Progress) PR or draft PR
4. Please use a semantic commits format title, such as `<type>[optional scope]: <description>`, see: https://go-kratos.dev/docs/community/contribution#type
5. at the same time, please note that similar work should be submitted in one PR as far as possible to reduce the workload of reviewers. Do not split a work into multiple PR unless it should.
-->

<!--
🎉 感谢您向 Kratos 发送 PR 请求！以下是一些提示：
如果这是你第一次为 Kratos 贡献，请阅读我们的贡献指南：https://go-kratos.dev/en/docs/community/contribution/
2、确保您已经为您的 PR 添加或运行了适当的测试和lint，请在提交PR之前使用“make lint”和“make test”，使用“make clean”整理您的 go.mod。
3、如果请购单未完成，您可能需要将其标记为 WIP（在制品）PR 或 draft PR
4、请使用语义提交格式标题，如“<类型>[可选范围]：<说明>`，请参阅：https://go-kratos.dev/docs/community/contribution#type
5. 同时请注意，同类的工作请尽量在一个PR中提交，以减轻 review 者的工作负担，不要把一项工作拆分成很多个PR，除非它应该这样做。
-->

<!--
-->

#### Description (what this PR does / why we need it):
<!--
* The description should include the motivation for this PR or contrast this with previous behavior
-->
read value from etcd twice if no such opOption

#### Which issue(s) this PR fixes (resolves / be part of):
<!--
* Automatically closes linked issue when PR is merged.
* If your PR is not fully resolved the issue, please use `part of #<issue number>` instead.

Usage: `fixes/resolves #<issue number>`, or `fixes/resolves (paste link of issue)`.
-->


#### Other special notes for the reviewers:
<!--
* Somethings that need extra attention for the reviewers
* Some additional notes, TODO list, etc.
-->


codecov-commenter: # [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/2146?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#2146](https://codecov.io/gh/go-kratos/kratos/pull/2146?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (91d053d) into [main](https://codecov.io/gh/go-kratos/kratos/commit/4ca25e46799ca1579d9e877e5be23f454ca23299?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (4ca25e4) will **not change** coverage.
> The diff coverage is `n/a`.

> :exclamation: Current head 91d053d differs from pull request most recent head e60ed87. Consider uploading reports for the commit e60ed87 to get more accurate results

```diff
@@           Coverage Diff           @@
##             main    #2146   +/-   ##
=======================================
  Coverage   79.08%   79.08%           
=======================================
  Files          84       84           
  Lines        3696     3696           
=======================================
  Hits         2923     2923           
  Misses        560      560           
  Partials      213      213           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/go-kratos/kratos/pull/2146?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/2146?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Last update [4ca25e4...e60ed87](https://codecov.io/gh/go-kratos/kratos/pull/2146?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).


Patch:
diff --git a/contrib/registry/etcd/watcher.go b/contrib/registry/etcd/watcher.go
index d04f4fbd..22e63474 100644
--- a/contrib/registry/etcd/watcher.go
+++ b/contrib/registry/etcd/watcher.go
@@ -29,7 +29,7 @@ func newWatcher(ctx context.Context, key, name string, client *clientv3.Client)
 		serviceName: name,
 	}
 	w.ctx, w.cancel = context.WithCancel(ctx)
-	w.watchChan = w.watcher.Watch(w.ctx, key, clientv3.WithPrefix(), clientv3.WithRev(0))
+	w.watchChan = w.watcher.Watch(w.ctx, key, clientv3.WithPrefix(), clientv3.WithRev(0), clientv3.WithKeysOnly())
 	err := w.watcher.RequestProgress(context.Background())
 	if err != nil {
 		return nil, err


Test patch:
diff --git a/contrib/log/fluent/fluent_test.go b/contrib/log/fluent/fluent_test.go
index 8558f31c..5b7b3e17 100644
--- a/contrib/log/fluent/fluent_test.go
+++ b/contrib/log/fluent/fluent_test.go
@@ -5,6 +5,7 @@ import (
 	"net"
 	"os"
 	"testing"
+	"time"
 
 	"github.com/go-kratos/kratos/v2/log"
 )
@@ -29,6 +30,96 @@ func TestMain(m *testing.M) {
 	os.Exit(m.Run())
 }
 
+func TestWithTimeout(t *testing.T) {
+	opts := new(options)
+	var duration time.Duration = 1000000000
+	funcTimeout := WithTimeout(duration)
+	funcTimeout(opts)
+	if opts.timeout != duration {
+		t.Errorf("WithTimeout() = %v, want %v", opts.timeout, duration)
+	}
+}
+
+func TestWithWriteTimeout(t *testing.T) {
+	opts := new(options)
+	var duration time.Duration = 1000000000
+	funcWriteTimeout := WithWriteTimeout(duration)
+	funcWriteTimeout(opts)
+	if opts.writeTimeout != duration {
+		t.Errorf("WithWriteTimeout() = %v, want %v", opts.writeTimeout, duration)
+	}
+}
+
+func TestWithBufferLimit(t *testing.T) {
+	opts := new(options)
+	bufferLimit := 1000000000
+	funcBufferLimit := WithBufferLimit(bufferLimit)
+	funcBufferLimit(opts)
+	if opts.bufferLimit != bufferLimit {
+		t.Errorf("WithBufferLimit() = %d, want %d", opts.bufferLimit, bufferLimit)
+	}
+}
+
+func TestWithRetryWait(t *testing.T) {
+	opts := new(options)
+	retryWait := 1000000000
+	funcRetryWait := WithRetryWait(retryWait)
+	funcRetryWait(opts)
+	if opts.retryWait != retryWait {
+		t.Errorf("WithRetryWait() = %d, want %d", opts.retryWait, retryWait)
+	}
+}
+
+func TestWithMaxRetry(t *testing.T) {
+	opts := new(options)
+	maxRetry := 1000000000
+	funcMaxRetry := WithMaxRetry(maxRetry)
+	funcMaxRetry(opts)
+	if opts.maxRetry != maxRetry {
+		t.Errorf("WithMaxRetry() = %d, want %d", opts.maxRetry, maxRetry)
+	}
+}
+
+func TestWithMaxRetryWait(t *testing.T) {
+	opts := new(options)
+	maxRetryWait := 1000000000
+	funcMaxRetryWait := WithMaxRetryWait(maxRetryWait)
+	funcMaxRetryWait(opts)
+	if opts.maxRetryWait != maxRetryWait {
+		t.Errorf("WithMaxRetryWait() = %d, want %d", opts.maxRetryWait, maxRetryWait)
+	}
+}
+
+func TestWithTagPrefix(t *testing.T) {
+	opts := new(options)
+	tagPrefix := "tag_prefix"
+	funcTagPrefix := WithTagPrefix(tagPrefix)
+	funcTagPrefix(opts)
+	if opts.tagPrefix != tagPrefix {
+		t.Errorf("WithTagPrefix() = %s, want %s", opts.tagPrefix, tagPrefix)
+	}
+}
+
+func TestWithAsync(t *testing.T) {
+	opts := new(options)
+	async := true
+	funcAsync := WithAsync(async)
+	funcAsync(opts)
+	if opts.async != async {
+		t.Errorf("WithAsync() = %t, want %t", opts.async, async)
+	}
+}
+
+func TestWithForceStopAsyncSend(t *testing.T) {
+	opts := new(options)
+	forceStopAsyncSend := true
+	funcForceStopAsyncSend := WithForceStopAsyncSend(forceStopAsyncSend)
+	funcForceStopAsyncSend(opts)
+	if opts.forceStopAsyncSend != forceStopAsyncSend {
+		t.Errorf("WithForceStopAsyncSend() = %t, want %t", opts.forceStopAsyncSend, forceStopAsyncSend)
+	}
+}
+
 func TestLogger(t *testing.T) {
 	logger, err := NewLogger("tcp://127.0.0.1:24224")
 	if err != nil {
@@ -43,6 +134,38 @@ func TestLogger(t *testing.T) {
 	flog.Error("log", "test")
 }
 
+func TestLoggerWithOpt(t *testing.T) {
+	var duration time.Duration = 1000000000
+	logger, err := NewLogger("tcp://127.0.0.1:24224", WithTimeout(duration))
+	if err != nil {
+		t.Error(err)
+	}
+	defer logger.Close()
+	flog := log.NewHelper(logger)
+
+	flog.Debug("log", "test")
+	flog.Info("log", "test")
+	flog.Warn("log", "test")
+	flog.Error("log", "test")
+}
+
+func TestLoggerError(t *testing.T) {
+	errCase := []string{
+		"foo",
+		"tcp://127.0.0.1/",
+		"tcp://127.0.0.1:1234a",
+		"tcp://127.0.0.1:65535",
+		"https://127.0.0.1:8080",
+		"unix://foo/bar",
+	}
+	for _, errc := range errCase {
+		_, err := NewLogger(errc)
+		if err == nil {
+			t.Error(err)
+		}
+	}
+}
+
 func BenchmarkLoggerPrint(b *testing.B) {
 	b.SetParallelism(100)
 	logger, err := NewLogger("tcp://127.0.0.1:24224")