Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 delete: atomic counter #5

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/metrica/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 0 additions & 26 deletions counter.go

This file was deleted.

21 changes: 0 additions & 21 deletions counter_test.go

This file was deleted.

10 changes: 1 addition & 9 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 2 additions & 46 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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++ {
Expand Down
Loading