From c7daedd8dedd5e28d9a2b2fac98b260cadd2e04d Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 11 Oct 2023 20:26:52 -0300 Subject: [PATCH] :bug: delete: atomic counter --- cmd/metrica/main.go | 3 +-- counter.go | 26 ------------------------ counter_test.go | 21 -------------------- handler.go | 10 +--------- handler_test.go | 48 ++------------------------------------------- 5 files changed, 4 insertions(+), 104 deletions(-) delete mode 100644 counter.go delete mode 100644 counter_test.go diff --git a/cmd/metrica/main.go b/cmd/metrica/main.go index 471d4fe..904a5a5 100644 --- a/cmd/metrica/main.go +++ b/cmd/metrica/main.go @@ -10,9 +10,8 @@ import ( ) func main() { - c := metrica.NewAtomicCounter() fs := metrica.NewFileStorage(&sync.Mutex{}, "metrica.txt") - mux := metrica.Handler(c, fs) + mux := metrica.Handler(fs) slog.Info("Starting server", "port", 8080) err := http.ListenAndServe(":8080", mux) diff --git a/counter.go b/counter.go deleted file mode 100644 index 8422baf..0000000 --- a/counter.go +++ /dev/null @@ -1,26 +0,0 @@ -// Package metrica provides the AtomicCounter implementation, that is used to increment the counter for concurrent requests. -package metrica - -import ( - "sync/atomic" -) - -// AtomicCounter is a struct to be used as a counter for concurrent requests. -type AtomicCounter struct { - count int64 -} - -// NewAtomicCounter returns a new AtomicCounter. -func NewAtomicCounter() *AtomicCounter { - return &AtomicCounter{} -} - -// Inc increments the counter by v. -func (c *AtomicCounter) Inc(v int64) { - atomic.AddInt64(&c.count, v) -} - -// Value returns the current value of the counter. -func (c *AtomicCounter) Value() int64 { - return atomic.LoadInt64(&c.count) -} diff --git a/counter_test.go b/counter_test.go deleted file mode 100644 index fa4f6e7..0000000 --- a/counter_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package metrica - -import ( - "sync" - "testing" -) - -func TestAtomicCounter(t *testing.T) { - var wg sync.WaitGroup - c := NewAtomicCounter() - for i := 0; i < 100; i++ { - wg.Add(1) - go func() { - c.Inc(1) - defer wg.Done() - }() - } - wg.Wait() - - assert(t, 100, int(c.Value())) -} diff --git a/handler.go b/handler.go index 0810c4e..a9cfafc 100644 --- a/handler.go +++ b/handler.go @@ -14,22 +14,14 @@ type countResponse struct { } // Handler returns a http.Handler for new endpoints. -func Handler(c *AtomicCounter, fs *FileStorage) http.Handler { +func Handler(fs *FileStorage) http.Handler { mux := http.NewServeMux() - mux.HandleFunc("/count", func(w http.ResponseWriter, r *http.Request) { - counter(w, r, c) - }) mux.HandleFunc("/countfs", func(w http.ResponseWriter, r *http.Request) { counterFs(w, r, fs) }) return mux } -func counter(w http.ResponseWriter, _ *http.Request, c *AtomicCounter) { - c.Inc(1) - send(w, http.StatusOK, countResponse{Count: c.Value()}) -} - func counterFs(w http.ResponseWriter, _ *http.Request, fs *FileStorage) { var wg sync.WaitGroup var counter int64 diff --git a/handler_test.go b/handler_test.go index ddae6ef..f991d19 100644 --- a/handler_test.go +++ b/handler_test.go @@ -9,50 +9,6 @@ import ( "testing" ) -func TestHandlerCount_Sequential(t *testing.T) { - c := NewAtomicCounter() - mux := Handler(c, nil) - - for i := 0; i < 100; i++ { - req := httptest.NewRequest("GET", "/count", nil) - w := httptest.NewRecorder() - mux.ServeHTTP(w, req) - if w.Code != http.StatusOK { - t.Errorf("expected status OK; got %v", w.Code) - } - - var gotRes countResponse - if err := json.NewDecoder(w.Body).Decode(&gotRes); err != nil { - t.Errorf("unable to decode body: %v", err) - } - - wantCount := int64(i + 1) - assert(t, wantCount, gotRes.Count) - } -} - -func TestHandlerCount_Concurrent(t *testing.T) { - c := NewAtomicCounter() - mux := Handler(c, nil) - var wg sync.WaitGroup - - for i := 0; i < 1000; i++ { - wg.Add(1) - go func() { - req := httptest.NewRequest("GET", "/count", nil) - w := httptest.NewRecorder() - mux.ServeHTTP(w, req) - if w.Code != http.StatusOK { - t.Errorf("expected status OK; got %v", w.Code) - } - defer wg.Done() - }() - } - wg.Wait() - - assert(t, int64(1000), c.Value()) -} - func TestHandlerCountFS_Sequential(t *testing.T) { f, err := os.Create("test") if err != nil { @@ -63,7 +19,7 @@ func TestHandlerCountFS_Sequential(t *testing.T) { }() fs := NewFileStorage(&sync.Mutex{}, f.Name()) - mux := Handler(nil, fs) + mux := Handler(fs) for i := 0; i < 100; i++ { req := httptest.NewRequest("GET", "/countfs", nil) @@ -94,7 +50,7 @@ func TestHandlerCountFS_Concurrent(t *testing.T) { fs := NewFileStorage(&sync.Mutex{}, f.Name()) - mux := Handler(nil, fs) + mux := Handler(fs) var wg sync.WaitGroup for i := 0; i < 100; i++ {