From 90fad95b2cbd2baf1b092aecbb639dac9cc65a2e Mon Sep 17 00:00:00 2001 From: PotatoCloud <60210021+PotatoCloud@users.noreply.github.com> Date: Tue, 30 Jan 2024 14:42:00 +0800 Subject: [PATCH 1/2] fix. testing file --- pkg/httputil/handler_test.go | 26 --------------------- pkg/tlsutil/tls_test.go | 45 ------------------------------------ 2 files changed, 71 deletions(-) delete mode 100644 pkg/httputil/handler_test.go delete mode 100644 pkg/tlsutil/tls_test.go diff --git a/pkg/httputil/handler_test.go b/pkg/httputil/handler_test.go deleted file mode 100644 index 094bb7b..0000000 --- a/pkg/httputil/handler_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package httputil_test - -import ( - "github.com/RealFax/RedQueen/pkg/httputil" - "net/http" - "testing" -) - -func TestNewBasicAuth(t *testing.T) { - users := map[string]string{ - "root": "toor", - "admin": "P@ssw0rd", - } - mux := http.NewServeMux() - - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Authed")) - }) - - srv := &http.Server{ - Addr: "localhost:8080", - Handler: httputil.NewBasicAuth(mux, httputil.NewMemoryBasicAuthFunc(users)), - } - - srv.ListenAndServe() -} diff --git a/pkg/tlsutil/tls_test.go b/pkg/tlsutil/tls_test.go deleted file mode 100644 index af4c66b..0000000 --- a/pkg/tlsutil/tls_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package tlsutil_test - -import ( - "crypto/tls" - "crypto/x509/pkix" - "github.com/RealFax/RedQueen/pkg/tlsutil" - "net/http" - "testing" -) - -func TestGenX509KeyPair(t *testing.T) { - cert, err := tlsutil.GenX509KeyPair(pkix.Name{ - CommonName: "localhost", - Country: []string{"Russian"}, - Organization: []string{"RealFax"}, - OrganizationalUnit: []string{"OpenSource"}, - }) - if err != nil { - t.Fatal(err) - } - - m := &http.ServeMux{} - m.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { - writer.Write([]byte("Hello, TLS")) - }) - - cfg := &tls.Config{ - NextProtos: []string{"http/1.1"}, - Certificates: []tls.Certificate{cert}, - InsecureSkipVerify: true, - } - - srv := &http.Server{ - Addr: "localhost:8080", - Handler: m, - TLSConfig: cfg, - } - - listener, err := tls.Listen("tcp", "localhost:8080", cfg) - if err != nil { - t.Fatal(err) - } - - srv.Serve(listener) -} From 1f311e307e61bc83d66f6953151b88ceebb54ee4 Mon Sep 17 00:00:00 2001 From: PotatoCloud <60210021+PotatoCloud@users.noreply.github.com> Date: Tue, 30 Jan 2024 14:55:18 +0800 Subject: [PATCH 2/2] fix. testing file --- internal/rqd/raft_log_apply_test.go | 62 +---------------------------- pkg/collapsar/writer.go | 5 ++- 2 files changed, 5 insertions(+), 62 deletions(-) diff --git a/internal/rqd/raft_log_apply_test.go b/internal/rqd/raft_log_apply_test.go index 26206a4..6b5c7a2 100644 --- a/internal/rqd/raft_log_apply_test.go +++ b/internal/rqd/raft_log_apply_test.go @@ -1,19 +1,9 @@ package rqd_test import ( - "bytes" - "context" + "github.com/RealFax/RedQueen/api/serverpb" red "github.com/RealFax/RedQueen/internal/rqd" - "github.com/RealFax/RedQueen/pkg/collapsar" - "io" - "strconv" "testing" - "time" - - "github.com/hashicorp/raft" - "google.golang.org/protobuf/proto" - - "github.com/RealFax/RedQueen/api/serverpb" ) var raftLogPayloadMessage = &serverpb.RaftLogPayload{ @@ -30,56 +20,6 @@ func TestRaftLogPayloadKey(t *testing.T) { t.Logf("Payload key: %d", red.RaftLogPayloadKey(raftLogPayloadMessage)) } -func TestRaftMultipleLogApply_Apply(t *testing.T) { - rm := red.NewRaftMultipleLogApply( - context.Background(), - 3, - time.Millisecond*300, - time.Millisecond*300, - func(cmd []byte, timeout time.Duration) raft.ApplyFuture { - r, err := collapsar.NewReader(bytes.NewReader(cmd[4:])) - if err != nil { - t.Fatal(err) - } - - for { - pack, rErr := r.Next() - if rErr != nil { - if rErr == io.EOF { - break - } - t.Fatal(err) - } - m := &serverpb.RaftLogPayload{} - if err = proto.Unmarshal(pack, m); err != nil { - t.Fatal(err) - } - t.Log(m.String()) - } - return nil - }) - - for i := 0; i < 3; i++ { - ctx := context.Background() - if err := rm.Apply(&ctx, raftLogPayloadMessage, 0); err != nil { - t.Fatal(err) - } - } - - time.Sleep(time.Second * 1) - - for i := 0; i < 3; i++ { - ctx := context.Background() - raftLogPayloadMessage.Key = []byte("test_key" + strconv.Itoa(i)) - if err := rm.Apply(&ctx, raftLogPayloadMessage, 0); err != nil { - t.Fatal(err) - } - } - - time.Sleep(time.Second * 1) - -} - func BenchmarkRaftLogPayloadKey(b *testing.B) { for i := 0; i < b.N; i++ { red.RaftLogPayloadKey(raftLogPayloadMessage) diff --git a/pkg/collapsar/writer.go b/pkg/collapsar/writer.go index 21a87c6..7f05e3e 100644 --- a/pkg/collapsar/writer.go +++ b/pkg/collapsar/writer.go @@ -21,7 +21,10 @@ func (a *writer) onFullTrigger() { if atomic.LoadUint32(&a.state) == 0 || atomic.LoadInt32(&a.max) != atomic.LoadInt32(&a.size) { return } - _ = a.Close() + + a.mu.Lock() + close(a.onFull) + a.mu.Unlock() } func (a *writer) Encode(w io.Writer) error {