diff --git a/db/mongo_test.go b/db/mongo_test.go index c203b94..9836dd4 100644 --- a/db/mongo_test.go +++ b/db/mongo_test.go @@ -39,6 +39,27 @@ type WorkoutDto struct { Year int `json:"year"` } +func TestGetCats(t *testing.T) { + client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://172.17.0.2:27017/cats")) + if err != nil { + log.Printf("Error connecting to MongoDB: %v", err) + } + + defer func() { + if err := client.Disconnect(context.TODO()); err != nil { + panic(err) + } + }() + + coll := client.Database("cats").Collection("cats") + + var result bson.M + + coll.FindOne(context.TODO(), bson.D{{"age", 2}}).Decode(&result) + + println(result) +} + func TestGetWorkouts(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() diff --git a/fss/git_fs.go b/fss/git_fs.go index 4b089c3..facce3d 100644 --- a/fss/git_fs.go +++ b/fss/git_fs.go @@ -2,7 +2,7 @@ package fss import ( "encoding/json" - "io/ioutil" + "os" "time" ) @@ -116,7 +116,7 @@ type Repo struct { func ReadJsonGT() []Repo { var repos []Repo pth := "/home/malandr/Documents/repos.json" - file, err := ioutil.ReadFile(pth) + file, err := os.ReadFile(pth) if err != nil { return nil } diff --git a/fss/read_lorem.go b/fss/read_lorem.go index c255856..b074969 100644 --- a/fss/read_lorem.go +++ b/fss/read_lorem.go @@ -8,10 +8,10 @@ import ( "time" ) -const DIR_NAME = "/home/malandr/Documents/" +const DirName = "/home/andrii/Documents/" func TestReadDocs(t *testing.T) { - docsDir, err := os.ReadDir(DIR_NAME) + docsDir, err := os.ReadDir(DirName) if err != nil { return } @@ -23,7 +23,7 @@ func TestReadDocs(t *testing.T) { for i := 1; i <= 400; i++ { wg.Add(1) go func(num int) { - fileBts, err := os.ReadFile(fmt.Sprintf("%s%s", DIR_NAME, docsDir[num].Name())) + fileBts, err := os.ReadFile(fmt.Sprintf("%s%s", DirName, docsDir[num].Name())) if err != nil { fmt.Println(err) } diff --git a/go.mod b/go.mod index 7ab470b..dbe4783 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module go-tests -go 1.22 - -toolchain go1.23.4 +go 1.23 require ( github.com/apache/pulsar-client-go v0.10.0 diff --git a/net/get_urls_sync_test.go b/net/get_urls_sync_test.go index f03ca76..920adbf 100644 --- a/net/get_urls_sync_test.go +++ b/net/get_urls_sync_test.go @@ -1,7 +1,7 @@ package net import ( - "io/ioutil" + "io" "log" "net/http" "sync" @@ -27,7 +27,7 @@ func GetData(url string, wg *sync.WaitGroup) { if err != nil { log.Fatalln(err) } - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } diff --git a/regexp_test.go b/regexp_test.go index 44bd661..ea1144f 100644 --- a/regexp_test.go +++ b/regexp_test.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "regexp" + "runtime" "strings" "testing" ) @@ -19,12 +20,7 @@ func TestReadLogFile(t *testing.T) { lines := strings.Split(string(logData), "\n") - //var m runtime.MemStats - //runtime.ReadMemStats(&m) - //fmt.Printf("Alloc = %s", formatBytes(m.Alloc)) - //fmt.Printf("\tTotalAlloc = %s", formatBytes(m.TotalAlloc)) - //fmt.Printf("\tSys = %s", formatBytes(m.Sys)) - //fmt.Printf("\tNumGC = %v\n", m.NumGC) + //memStats() userNames := make(map[string]struct{}) @@ -73,7 +69,12 @@ func ReadLineByLine() { return } // 2. Ensure the file is closed at the end of the function - defer file.Close() + defer func(file *os.File) { + err := file.Close() + if err != nil { + return + } + }(file) // 3. Create a new Scanner for the file scanner := bufio.NewScanner(file) @@ -93,10 +94,9 @@ func ReadLineByLine() { func TestParseLogs(t *testing.T) { - // var regExp = regexp.MustCompile("^(\\d+\\.\\d+\\.\\d+\\.\\d+) (.*) (.*) (\\[.*\\]) (\".*\") (\".*\") (\".*\")$") - data := "127.0.0.1 158x6583999x2 a.navadiya [08/Mar/2025:02:38:51 -0800] \"GET /rest/internal/2.0/client- HTTP/1.0\" 200 18 6 \"https://jira.ontrq.com/secure/Dashboard.jspa?selectPageId=83502\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36\" \"1sx1d6m\"" + data := "127.0.0.1 158x6583999x2 a.navaddi [08/Mar/2025:02:38:51 -0800] \"GET /rest/internal/2.0/client- HTTP/1.0\" 200 18 6 \"https://jira.x.com/secure/Dashboard.jspa?selectPageId=83502\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36\" \"1sx1d6m\"" matchString := regExp.MatchString(data) @@ -110,7 +110,6 @@ func TestParseLogs(t *testing.T) { //requestIdx := 4 //clientIdx := 5 //sessionIdx := 6 - // //ipData := string(submatch[0][ipIdx]) //reqId := string(submatch[0][reqIdx]) //username := string(submatch[0][userNameIdx]) @@ -126,3 +125,12 @@ func TestParseLogs(t *testing.T) { } } + +func memStats() { + var m runtime.MemStats + runtime.ReadMemStats(&m) + fmt.Printf("Alloc = %s", formatBytes(m.Alloc)) + fmt.Printf("\tTotalAlloc = %s", formatBytes(m.TotalAlloc)) + fmt.Printf("\tSys = %s", formatBytes(m.Sys)) + fmt.Printf("\tNumGC = %v\n", m.NumGC) +}