From a61d928b279c5c508aca3bfc7cb14d810c3d75de Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Tue, 20 May 2025 07:19:33 +0200 Subject: vault backup: 2025-05-20 07:19:33 --- .trash/OVN Clusters 2.md | 0 ... para and the words Parametric and Parameter.md | 9 +++++ Benchmarking code in Golang.md | 7 ++-- Cleaning the Nix store.md | 2 + OVN upgrade playbook.md | 2 + Parallellizing go tests.md | 7 ++++ Redirecting output during testing.md | 43 ++++++++++++++++++++++ Table driven tests.md | 14 +++++++ Testing Golang programs.md | 1 + Timers.md | 7 ++++ daily/17-May-2025.md | 28 ++++++++++++++ daily/18-May-2025.md | 12 ++++++ daily/19-May-2025.md | 12 ++++++ 13 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 .trash/OVN Clusters 2.md create mode 100644 .trash/The meaning of para and the words Parametric and Parameter.md create mode 100644 Parallellizing go tests.md create mode 100644 Redirecting output during testing.md create mode 100644 Table driven tests.md create mode 100644 Timers.md create mode 100644 daily/17-May-2025.md create mode 100644 daily/18-May-2025.md create mode 100644 daily/19-May-2025.md diff --git a/.trash/OVN Clusters 2.md b/.trash/OVN Clusters 2.md new file mode 100644 index 0000000..e69de29 diff --git a/.trash/The meaning of para and the words Parametric and Parameter.md b/.trash/The meaning of para and the words Parametric and Parameter.md new file mode 100644 index 0000000..a0f009c --- /dev/null +++ b/.trash/The meaning of para and the words Parametric and Parameter.md @@ -0,0 +1,9 @@ +--- +tags: + - language +--- +"Para" comes from Greek and has different meanings in different contexts. For example we also see it in "Paranormal" where "para" stands for something as "irregular" or "abnormal". +"Paralegal" or "paramedic" it mean "ancillary" which means to provide support (ancillary system == support system). +It can also mean "protection against" as in "parasol" or "parachute". + +In our case of "parametric" and "parameter" \ No newline at end of file diff --git a/Benchmarking code in Golang.md b/Benchmarking code in Golang.md index 22d180d..9bf9cfd 100644 --- a/Benchmarking code in Golang.md +++ b/Benchmarking code in Golang.md @@ -17,17 +17,16 @@ func main() { Writing benchmarks. They look similar to tests: ``` -import testing +import "testing" func BenchmarkSome(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { Some() } } ``` -b.N is supplied by test driver and is dynamically changed depending on runtime. -`go test -bench=` +`go test -bench=` (thus `-bench=.` will run all tests) This just benchmarks a function being called many times, comparative benchmarks can be written using a regular function being called from Benchmarks, something like so : ``` diff --git a/Cleaning the Nix store.md b/Cleaning the Nix store.md index 523cb71..0dab645 100644 --- a/Cleaning the Nix store.md +++ b/Cleaning the Nix store.md @@ -3,7 +3,9 @@ tags: - nix - howto --- + ``` +nix-env --delete-generations 14d sudo nix-collect-garbage -d sudo nixos-rebuild boot ``` diff --git a/OVN upgrade playbook.md b/OVN upgrade playbook.md index c305325..a5c5818 100644 --- a/OVN upgrade playbook.md +++ b/OVN upgrade playbook.md @@ -2,6 +2,8 @@ tags: - groupvps --- +[[OVN upgrade]] + 1. Test failover. 1. Start an xping 2. Log onto n01 network vps4 diff --git a/Parallellizing go tests.md b/Parallellizing go tests.md new file mode 100644 index 0000000..78afda5 --- /dev/null +++ b/Parallellizing go tests.md @@ -0,0 +1,7 @@ +--- +tags: + - testing +--- +[[Table driven tests]] [[Testing Golang programs]] + +We can call t.Parallel() to indicate our test can be run in parallel. This can also be done if we have a test function that executes more test cases via t.Run() because that simply runs a test function we can also call t.Parallel() inside of it. \ No newline at end of file diff --git a/Redirecting output during testing.md b/Redirecting output during testing.md new file mode 100644 index 0000000..f4eda0b --- /dev/null +++ b/Redirecting output during testing.md @@ -0,0 +1,43 @@ +--- +tags: + - golang + - testing +--- +[[Testing Golang programs]] + +Some functions print or log to stdout/stderr, but during testing we would like to capture this. Here is an example of how to do that: +```echo.go +package echo + +import ( + "fmt" + "io" + "os" + "strings" +) + +var out io.Writer = os.Stdout + +func echo() { + fmt.Fprintln(out, strings.Join(os.Args[1:], " ")) +} +``` + +We define an io.Writer at the package level which is used to write our output to. During tests we can set this to be a buffer, ie: + +```echo_test.go +package echo + +import ( + "bytes" + "testing" +) + +func BenchmarkEcho(b *testing.B) { + out = new(bytes.Buffer) + + for b.Loop() { + echo() + } +} +``` \ No newline at end of file diff --git a/Table driven tests.md b/Table driven tests.md new file mode 100644 index 0000000..e2b2f3a --- /dev/null +++ b/Table driven tests.md @@ -0,0 +1,14 @@ +--- +tags: + - testing +--- +https://go.dev/wiki/TableDrivenTests +--- + +[[Testing Golang programs]] [[Test fixture]] + +--- + +Sign when to use: copy and pasting tests. +Similar to what we do with phpunit/prophecy : providing a dataProvider. +In go we can just define a slice of structs or a map and loop over it in our test function. \ No newline at end of file diff --git a/Testing Golang programs.md b/Testing Golang programs.md index e9d48de..a97f734 100644 --- a/Testing Golang programs.md +++ b/Testing Golang programs.md @@ -2,6 +2,7 @@ tags: - golang - howto + - testing --- `go test` reads files ending with `_test.go`: 3 specialized functions: - Test functions; names start with Test, 1 arg. \*testing.T diff --git a/Timers.md b/Timers.md new file mode 100644 index 0000000..804477e --- /dev/null +++ b/Timers.md @@ -0,0 +1,7 @@ +--- +tags: + - timer +--- +Timers created on online-stopwatch.com can be exported as an URL. + +- [Kettlebell workout 1](https://www.online-stopwatch.com/full-screen-interval-timer/?c=r221pn1nwx) \ No newline at end of file diff --git a/daily/17-May-2025.md b/daily/17-May-2025.md new file mode 100644 index 0000000..3729110 --- /dev/null +++ b/daily/17-May-2025.md @@ -0,0 +1,28 @@ +# Notes on go +Go compiles into native machine code. +Go handles Unicode natively thus any character can be used in a string. +Divided into packages, where a package is sort of a library, except for main which defines an executable. +Import declarations **must** follow the package declaration. +Function declarations: `func () ()`. No semicolons, unless multiple statements on 1 line. +`gofmt` mandatory. `goimports` (`golang.org/x/tools/cmd/goimports`) manages imports. +Slices are dynamically sized arrays that can be accessed `s[i]` or a subset can be taken `s[i:j]`. Indexing: half-open (starts at 0). `s[i:j]` yields i through j-1. Either can be omitted. + +Convention: comment describing package preceding package declaration. + +Variables initialised to "zero value".7689 +String concatenation via `+` char. +Supports assignment operators, e.g `+=`, `*=` etc +Short-hand variable declaration requires no type, it is inferred of the value `:=` **only allowed in a function** + +For-loop structure: +``` +for initialization; condition; post { + // code +} +``` +Either part can be left away with different semantics. +All parts gone: loop forever. +Only condition is like a while x == true; +`range` keyword to loop over a slice or array with `for index, arg := range slice` + +`_` is the blank identifier, thus above if we don' need the index we can put `_`. \ No newline at end of file diff --git a/daily/18-May-2025.md b/daily/18-May-2025.md new file mode 100644 index 0000000..2ef64b9 --- /dev/null +++ b/daily/18-May-2025.md @@ -0,0 +1,12 @@ +Some programming problems or programs to make for practicing: +- Echo stdin +- Find duplicate lines on stdin or files passed as arguments. Operating on a stream, or slurp all input and do it at once. + +# Further notes on Go +Order of map iteration is unspecified but random in practice to prevent reliance on ordering. +The Scanner type from the bufio package provides an easy way to read input in lines or words. +Map created with `make` is passed by reference. +Package `io/ioutil` exposes `ReadFile` and other io utility functions and types. +Casting works by `type(var)`, e.g `i := 0; string(i)` +Referencing a multi-component package is done through the last component. E.g `ioutil` for the `io/ioutil` package. +Composite literals are the form of `type{...}` they instantiate composite types, so `[]string{"a"}` is also a composite literal. \ No newline at end of file diff --git a/daily/19-May-2025.md b/daily/19-May-2025.md new file mode 100644 index 0000000..aa9ab6b --- /dev/null +++ b/daily/19-May-2025.md @@ -0,0 +1,12 @@ +# Another note on Go +`image/color` package features the Color interface defining a Color as just a type that has a method `RGBA() (r, g, b, a uint32)`. It has a struct `RGBA` defining this method. So for example to define green we can say: `color.RGBA{0x00, 0xFF, 0x00, 0xFF}` R G B A respectively. + +Packaging doing stuff with networking are grouped under `net`, e.g `net/http` + +Using `io.Copy` we can copy output from a reader to a writer. This way we can for example copy a HTTP response directly to stdout instead of buffering the entire output first. + +`strings.HasPrefix` exists + +To get a time delta we can use `time.Since` which returns a `time.Time`. We can just call for example `Seconds()` on that to get the elapsed seconds. + +Goroutines run concurrently and communicate over channels, they are started with the `go` keyword. Sending or receiving on a channel block the goroutine. \ No newline at end of file -- cgit v1.2.3