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

Sprint 7, increment 16 #18

Merged
merged 5 commits into from
Dec 1, 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
6 changes: 3 additions & 3 deletions .github/workflows/mertricstest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

metricstest:
runs-on: ubuntu-latest
container: golang:1.20
container: golang:1.21
needs: branchtest

services:
Expand All @@ -38,10 +38,10 @@ jobs:
uses: actions/checkout@v2

- name: Download autotests binaries
uses: robinraju/release-downloader@v1.7
uses: robinraju/release-downloader@v1.8
with:
repository: Yandex-Practicum/go-autotests
tag: refactor
latest: true
fileName: "*"
out-file-path: .tools
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/statictest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ on:
jobs:
statictest:
runs-on: ubuntu-latest
container: golang:1.20
container: golang:1.21
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Download statictest binary
uses: robinraju/release-downloader@v1.7
uses: robinraju/release-downloader@v1.8
with:
repository: Yandex-Practicum/go-autotests
tag: refactor
latest: true
fileName: statictest
out-file-path: .tools
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/shirou/gopsutil/v3 v3.23.8
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63
)

require (
Expand All @@ -24,6 +23,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDN
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/shirou/gopsutil/v3 v3.23.8 h1:xnATPiybo6GgdRoC4YoGnxXZFRc3dqQTGi73oLvvBrE=
github.com/shirou/gopsutil/v3 v3.23.8/go.mod h1:7hmCaBn+2ZwaZOr6jmPBZDfawwMGuo1id3C6aM8EDqQ=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand Down
6 changes: 3 additions & 3 deletions internal/infra/compression/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func (w gzipWriter) Write(b []byte) (int, error) {

func MiddlewareWriter(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO: split string and check
// todo: split string and check
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
next.ServeHTTP(w, r)
return
}

gz, err := gzip.NewWriterLevel(w, gzip.BestSpeed)
gz, err := gzip.NewWriterLevel(w, gzip.BestCompression)
if err != nil {
io.WriteString(w, err.Error())
return
Expand All @@ -39,7 +39,7 @@ func MiddlewareWriter(next http.Handler) http.Handler {

func MiddlewareReader(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO: split string and check
// todo: split string and check
if !strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
next.ServeHTTP(w, r)
return
Expand Down
3 changes: 2 additions & 1 deletion internal/infra/config/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
DefRetryAttempts = 3
DefRetryIntervalInitial = 1 * time.Second
DefRetryIntervalBackoff = 2 * time.Second
DefRateLimit = 1
)

type Config struct {
Expand All @@ -41,7 +42,7 @@ func InitConfig() (*Config, error) {
)
pollInterval := flag.Uint("p", DefPollInterval, "How often to query metrics, seconds")
hmacKey := flag.String("k", "", "HMAC key for integrity checks")
rateLimit := flag.Uint("l", 1, "Max number of active workers")
rateLimit := flag.Uint("l", DefRateLimit, "Max number of active workers")
flag.Parse()
if conf.Addr == "" {
conf.Addr = *addr
Expand Down
4 changes: 4 additions & 0 deletions internal/infra/config/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestInitConfig(t *testing.T) {
RetryAttempts: DefRetryAttempts,
RetryIntervalBackoff: DefRetryIntervalBackoff,
RetryIntervalInitial: DefRetryIntervalInitial,
RateLimit: DefRateLimit,
},
},
{
Expand All @@ -41,6 +42,7 @@ func TestInitConfig(t *testing.T) {
RetryAttempts: DefRetryAttempts,
RetryIntervalBackoff: DefRetryIntervalBackoff,
RetryIntervalInitial: DefRetryIntervalInitial,
RateLimit: DefRateLimit,
},
},
{
Expand All @@ -55,6 +57,7 @@ func TestInitConfig(t *testing.T) {
RetryAttempts: DefRetryAttempts,
RetryIntervalBackoff: DefRetryIntervalBackoff,
RetryIntervalInitial: DefRetryIntervalInitial,
RateLimit: DefRateLimit,
},
},
{
Expand All @@ -69,6 +72,7 @@ func TestInitConfig(t *testing.T) {
RetryAttempts: DefRetryAttempts,
RetryIntervalBackoff: DefRetryIntervalBackoff,
RetryIntervalInitial: DefRetryIntervalInitial,
RateLimit: DefRateLimit,
},
},
}
Expand Down
112 changes: 112 additions & 0 deletions internal/infra/utils/retry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package utils

import (
"context"
"errors"
"reflect"
"testing"
"time"

"github.com/matthiasBT/monitoring/internal/infra/logging"
)

var ErrSome = errors.New("some error")

type UnreliableCallCounter struct {
called int
stopFailuresAt int
}

func (cc *UnreliableCallCounter) do() (any, error) {
cc.called += 1
if cc.called == cc.stopFailuresAt {
time.Sleep(100 * time.Millisecond)
return 100500, nil
}
return nil, ErrSome
}

func TestRetrier_RetryChecked(t *testing.T) {
type args struct {
timeout time.Duration
f func() (any, error)
checkError func(error) bool
}
tests := []struct {
name string
args args
want any
wantErr error
}{
{
name: "success_first_try",
args: args{
f: func() (any, error) { return 100500, nil },
checkError: nil,
},
want: 100500,
wantErr: nil,
},
{
name: "success_second_try",
args: args{
f: (&UnreliableCallCounter{stopFailuresAt: 2}).do,
checkError: func(err error) bool { return errors.Is(err, ErrSome) },
},
want: 100500,
wantErr: nil,
},
{
name: "failure_on_unchecked_error",
args: args{
f: (&UnreliableCallCounter{stopFailuresAt: 2}).do,
checkError: func(err error) bool { return !errors.Is(err, ErrSome) },
},
want: nil,
wantErr: ErrSome,
},
{
name: "number_of_attempts_exceeded",
args: args{
f: (&UnreliableCallCounter{stopFailuresAt: 5}).do,
checkError: func(err error) bool { return errors.Is(err, ErrSome) },
},
want: nil,
wantErr: ErrSome,
},
{
name: "timeout",
args: args{
f: (&UnreliableCallCounter{stopFailuresAt: 5}).do,
checkError: func(err error) bool { return errors.Is(err, ErrSome) },
timeout: 1 * time.Millisecond,
},
want: nil,
wantErr: ErrRetryAborted,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Retrier{
Attempts: 2,
IntervalFirst: 100 * time.Millisecond,
IntervalIncrease: 100 * time.Millisecond,
Logger: logging.SetupLogger(),
}
ctx := context.Background()
if tt.args.timeout != 0 {
ctxt, cancel := context.WithTimeout(ctx, tt.args.timeout)
ctx = ctxt
defer cancel()
}
got, err := r.RetryChecked(ctx, tt.args.f, tt.args.checkError)
if err != nil && !errors.Is(err, tt.wantErr) {
t.Errorf("RetryChecked() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("RetryChecked() got = %v, want %v", got, tt.want)
}
})
}
}
61 changes: 61 additions & 0 deletions internal/server/adapters/filekeeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package adapters

import (
"context"
"os"
"sync"
"testing"

common "github.com/matthiasBT/monitoring/internal/infra/entities"
"github.com/matthiasBT/monitoring/internal/infra/logging"
)

func TestFileKeeper_Flush(t *testing.T) {
tests := []struct {
name string
storageSnapshot []*common.Metrics
wantErr bool
}{
{
name: "flush_snapshot",
storageSnapshot: []*common.Metrics{
{
ID: "BarFoo1",
MType: common.TypeGauge,
Delta: nil,
Value: ptrfloat64(44.1),
},
{
ID: "BarFoo2",
MType: common.TypeGauge,
Delta: nil,
Value: ptrfloat64(55.5),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
file, err := os.CreateTemp("", "example")
if err != nil {
t.Fatal("Failed to create a temporary file")
}
defer os.Remove(file.Name())
defer file.Close()
fs := &FileKeeper{
Logger: logging.SetupLogger(),
Path: file.Name(),
Lock: &sync.Mutex{},
}
if err := fs.Flush(context.Background(), tt.storageSnapshot); err != nil {
t.Errorf("Flush() error = %v", err)
}
restoredState := fs.Restore()
if len(restoredState) != len(tt.storageSnapshot) ||
!compare(restoredState[0], tt.storageSnapshot[0]) ||
!compare(restoredState[1], tt.storageSnapshot[1]) {
t.Errorf("State after Restore() is not equal")
}
})
}
}
10 changes: 6 additions & 4 deletions internal/server/adapters/memstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
common "github.com/matthiasBT/monitoring/internal/infra/entities"
"github.com/matthiasBT/monitoring/internal/infra/logging"
"github.com/matthiasBT/monitoring/internal/server/entities"
"golang.org/x/exp/maps"
)

type State struct {
Expand Down Expand Up @@ -76,9 +75,12 @@ func (storage *MemStorage) GetAll(ctx context.Context) (map[string]*common.Metri
return storage.Metrics, nil
}

func (storage *MemStorage) Snapshot(ctx context.Context) ([]*common.Metrics, error) {
data := maps.Values(storage.Metrics)
return data, nil
func (storage *MemStorage) Snapshot(context.Context) ([]*common.Metrics, error) {
result := make([]*common.Metrics, 0, len(storage.Metrics))
for _, val := range storage.Metrics {
result = append(result, val)
}
return result, nil
}

func (storage *MemStorage) Init(data []*common.Metrics) {
Expand Down
Loading
Loading