diff --git a/.github/workflows/go-queue.yml b/.github/workflows/go-queue.yml index 58b435b..8e2c2b8 100644 --- a/.github/workflows/go-queue.yml +++ b/.github/workflows/go-queue.yml @@ -2,44 +2,68 @@ name: go-queue on: push: - branches: ["main"] - pull_request: - release: - types: [published] + branches: + - main + paths: + - "./" + - ".github/workflows/go-queue.yml" + +env: + LINTER_VERSION: "v1.51.2" + GO_VERSION: "1.19" jobs: - version: + environment: runs-on: ubuntu-latest + env: + GITHUB_SHA: ${{ github.sha }} + GITHUB_REF: ${{ github.ref }} steps: + - name: Get git source + run: | + git_source=`echo $GITHUB_REF | sed 's/refs\/heads\///'` + git_source=`echo $git_source | sed 's/refs\/tags\///'` + git_source=`echo $git_source | sed 's/refs\\///'` + echo $git_source + echo "git_source=$git_source" >> $GITHUB_ENV + - name: Get git SHA + run: | + git_sha=`echo $GITHUB_SHA` + echo $git_sha + echo "git_sha=$git_sha" >> $GITHUB_ENV - name: Checkout repository code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Get version run: | - version=`cat /home/runner/work/go-queue/go-queue/version.json | jq '.Version' | sed 's/"//g'` - echo $version - echo "version=$version" >> $GITHUB_ENV + version_source=`cat /home/runner/work/go-queue/go-queue/version.json | jq '.Version' | sed 's/"//g'` + echo $version_source + echo "version_source=$version_source" >> $GITHUB_ENV - name: Generate build artifacts run: | mkdir -p /tmp - echo ${{ env.version }} >> /tmp/version + echo ${{ env.version_source }} >> /tmp/version_source + echo ${{ env.git_source }} >> /tmp/git_source + echo ${{ env.git_sha }} >> /tmp/git_sha - name: Upload artifacts - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: artifacts path: | - /tmp/version + /tmp/version_source + /tmp/git_source + /tmp/git_sha go_lint: runs-on: ubuntu-latest steps: - uses: actions/setup-go@v3 with: - go-version: 1.16 - - uses: actions/checkout@v3 + go-version: ${{ env.GO_VERSION }} + - uses: actions/checkout@v4 - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: - version: v1.44.2 + version: ${{ env.LINTER_VERSION }} working-directory: . args: --verbose @@ -50,9 +74,9 @@ jobs: contents: read steps: - name: Check out repository code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v4 with: go-version: 1.16 - name: Test go-queue/finite @@ -68,7 +92,7 @@ jobs: go mod download go test -v ./... -coverprofile /tmp/go-queue-infinite.out > /tmp/go-queue-infinite.log - name: Upload artifacts - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: artifacts path: | @@ -79,28 +103,27 @@ jobs: retention-days: 1 git_push_tag: - if: github.ref == 'refs/heads/main' - needs: [go_test, version] + needs: [go_test, environment] runs-on: ubuntu-latest permissions: contents: write steps: - name: Download artifacts - uses: actions/download-artifact@v2 + uses: actions/download-artifact@v3 with: name: artifacts path: /tmp - name: Get version run: | - version=`cat /tmp/version` + version=`cat /tmp/version_source` echo "version=$version" >> $GITHUB_ENV - name: Check out repository code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: fetch-depth: "0" - - name: Push/Update go-queue version - uses: anothrNick/github-tag-action@1.36.0 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - WITH_V: true - CUSTOM_TAG: v${{ env.version }} + # - name: Push/Update go-queue version + # uses: anothrNick/github-tag-action@v1 + # env: + # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # WITH_V: true + # CUSTOM_TAG: v${{ env.version }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c32dba..5284fc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this service will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [1.2.3] - 03/19/22 + +- Fixed the TestQueue test such that it used the example properly and would work even if the items returned was a slice of bytes + ## [1.2.2] - 09/10/22 - Added "Must" functions that will block until success diff --git a/example.go b/example.go index c2ccf1f..da9d69e 100644 --- a/example.go +++ b/example.go @@ -6,7 +6,7 @@ import ( "reflect" ) -//REFERENCE: https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go +// REFERENCE: https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go func randomString(nLetters ...int) string { letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") nLetter := 20 @@ -21,8 +21,8 @@ func randomString(nLetters ...int) string { } type Example struct { - Int int `json:"int,omitempty"` - Float float64 `json:"float,omitempty"` + Int int `json:"int,omitempty,string"` + Float float64 `json:"float,omitempty,string"` String string `json:"string,omitempty"` } @@ -118,7 +118,7 @@ func ExampleFlush(queue Dequeuer) []*Example { return ExampleConvertMultiple(queue.Flush()) } -//ExampleGenFloat64 will generate a random number of random float values if n is equal to 0 +// ExampleGenFloat64 will generate a random number of random float values if n is equal to 0 // not to exceed the constant TestMaxExamples, if n is provided, it will generate that many items func ExampleGenFloat64(sizes ...int) []*Example { size := int(rand.Float64() * 100) diff --git a/infinite/tests/tests.go b/infinite/tests/tests.go index 05816f5..7f1ce82 100644 --- a/infinite/tests/tests.go +++ b/infinite/tests/tests.go @@ -12,20 +12,22 @@ import ( const casef string = "case: %s" -//TestEnqueue attempt to unit test the enqueue function, in general it confirms the +// TestEnqueue attempt to unit test the enqueue function, in general it confirms the // behavior, that for an infinite queue, no matter how much data you put into the // queue, the queue will never overflow and there will be no data loss. This test // also assumes that the "size" of the queue won't affect the behavior of enqueue // This test has some "configuration" items that can be "tweaked" for your specific // queue implementation: -// - rate: this is the rate at which the test will attempt to enqueue/dequeue -// - timeout: this is when the test will "give up" +// - rate: this is the rate at which the test will attempt to enqueue/dequeue +// - timeout: this is when the test will "give up" +// // Some assumptions this test does make: -// - your queue can handle valid data, as a plus the example data type supports the -// BinaryMarshaller +// - your queue can handle valid data, as a plus the example data type supports the +// BinaryMarshaller +// // Some assumptions this test won't make: -// - order is maintained -// - the "size" of the queue affects the behavior of enqueue +// - order is maintained +// - the "size" of the queue affects the behavior of enqueue func TestEnqueue(t *testing.T, rate, timeout time.Duration, newQueue func() interface { goqueue.Owner goqueue.Enqueuer @@ -86,19 +88,21 @@ func TestEnqueue(t *testing.T, rate, timeout time.Duration, newQueue func() inte } } -//TestEnqueueMultiple will attempt to unit test the EnqueueMultiple function; +// TestEnqueueMultiple will attempt to unit test the EnqueueMultiple function; // for an infinite queue, this function will never overflow nor will it return // items that weren't able to be enqueued. // This test has some "configuration" items that can be "tweaked" for your specific // queue implementation: -// - rate: this is the rate at which the test will attempt to enqueue/dequeue -// - timeout: this is when the test will "give up" +// - rate: this is the rate at which the test will attempt to enqueue/dequeue +// - timeout: this is when the test will "give up" +// // Some assumptions this test does make: -// - your queue can handle valid data, as a plus the example data type supports the -// BinaryMarshaller +// - your queue can handle valid data, as a plus the example data type supports the +// BinaryMarshaller +// // Some assumptions this test won't make: -// - order is maintained -// - the "size" of the queue affects the behavior of enqueue +// - order is maintained +// - the "size" of the queue affects the behavior of enqueue func TestEnqueueMultiple(t *testing.T, rate, timeout time.Duration, newQueue func() interface { goqueue.Owner goqueue.Enqueuer @@ -162,20 +166,22 @@ func TestEnqueueMultiple(t *testing.T, rate, timeout time.Duration, newQueue fun } } -//TestEnqueueInFront will validate that if there is data in the queue and you attempt to +// TestEnqueueInFront will validate that if there is data in the queue and you attempt to // enqueue in front, that special "data" will go to the front, while if the queue is empty // that data will just be "in" the queue (a regular queue if the queue is empty). // This test has some "configuration" items that can be "tweaked" for your specific // queue implementation: -// - rate: this is the rate at which the test will attempt to enqueue/dequeue -// - timeout: this is when the test will "give up" +// - rate: this is the rate at which the test will attempt to enqueue/dequeue +// - timeout: this is when the test will "give up" +// // Some assumptions this test does make: -// - your queue can handle valid data, as a plus the example data type supports the -// BinaryMarshaller -// - your queue maintains order -// - it's safe to use a single instance of your queue for each test case +// - your queue can handle valid data, as a plus the example data type supports the +// BinaryMarshaller +// - your queue maintains order +// - it's safe to use a single instance of your queue for each test case +// // Some assumptions this test won't make: -// - the "size" of the queue affects the behavior of enqueue +// - the "size" of the queue affects the behavior of enqueue func TestEnqueueInFront(t *testing.T, rate, timeout time.Duration, newQueue func() interface { goqueue.Owner goqueue.Enqueuer @@ -224,8 +230,7 @@ func TestEnqueueInFront(t *testing.T, rate, timeout time.Duration, newQueue func item, underflow := goqueue.MustDequeue(q, ctx.Done(), rate) cancel() assert.False(t, underflow) - assert.IsType(t, &goqueue.Example{}, item, casef, cDesc) - example, _ := item.(*goqueue.Example) + example := goqueue.ExampleConvertSingle(item) assert.Equal(t, c.iInFrontValue, example, casef, cDesc) //flush the queue to empty it @@ -238,7 +243,7 @@ func TestEnqueueInFront(t *testing.T, rate, timeout time.Duration, newQueue func } } -//TestEnqueueEvent will confirm that the signal channels function correctly when data is enqueued, +// TestEnqueueEvent will confirm that the signal channels function correctly when data is enqueued, // this function for an infinite queue is slightly different because it can't be lossless, there's // no way to properly implement a buffered channel with an infinite queue // Some assumptions this test does make: diff --git a/tests/tests.go b/tests/tests.go index 2394473..4d3e62f 100644 --- a/tests/tests.go +++ b/tests/tests.go @@ -37,7 +37,7 @@ func randomString(nLetters ...int) string { return string(b) } -//TestGarbageCollect attempts to validate that memory is returned to the heap and can be properly +// TestGarbageCollect attempts to validate that memory is returned to the heap and can be properly // garbage collected it executes and garbage collection is manually triggered. This makes some // heavy underlying assumptions that a slice or map data structure. The idea behind the function // is that it'll re-create those data structures with new maps/slices such that that the items @@ -92,20 +92,22 @@ func TestGarbageCollect(t *testing.T, rate, timeout time.Duration, newQueue func } } -//TestDequeue will confirm the functionality of the underflow output, with an infinite queue +// TestDequeue will confirm the functionality of the underflow output, with an infinite queue // the expectation is that the enqueue will never overflow, and the dequeue will only underflow // if the queue is empty. Although this use case is the "same" for infinite and finite queues // the dequeue function is based on the behavior of the enqueue function. This test has some // "configuration" items that can be "tweaked" for your specific queue implementation: -// - rate: this is the rate at which the test will attempt to enqueue/dequeue -// - timeout: this is when the test will "give up" +// - rate: this is the rate at which the test will attempt to enqueue/dequeue +// - timeout: this is when the test will "give up" +// // Some assumptions this test does make: -// - your queue can handle valid data, as a plus the example data type supports the -// BinaryMarshaller -// - your queue maintains order -// - it's safe to use a single instance of your queue for each test case +// - your queue can handle valid data, as a plus the example data type supports the +// BinaryMarshaller +// - your queue maintains order +// - it's safe to use a single instance of your queue for each test case +// // Some assumptions this test won't make: -// - the "size" of the queue affects the behavior of enqueue +// - the "size" of the queue affects the behavior of enqueue func TestDequeue(t *testing.T, rate, timeout time.Duration, newQueue func(size int) interface { goqueue.Owner goqueue.Enqueuer @@ -126,11 +128,10 @@ func TestDequeue(t *testing.T, rate, timeout time.Duration, newQueue func(size i //attempt to dequeue (confirm underflow) ctx, cancel := context.WithTimeout(context.TODO(), timeout) defer cancel() - //KIM: we don't confirm that the item is nil because it's - // inconsequential to this test - _, underflow := goqueue.MustDequeue(q, ctx.Done(), rate) + item, underflow := goqueue.MustDequeue(q, ctx.Done(), rate) cancel() assert.True(t, underflow) + assert.Nil(t, item) //enqueue items ctx, cancel = context.WithTimeout(context.TODO(), timeout) @@ -143,7 +144,6 @@ func TestDequeue(t *testing.T, rate, timeout time.Duration, newQueue func(size i ctx, cancel = context.WithTimeout(context.TODO(), timeout) defer cancel() item, underflow := goqueue.MustDequeue(q, ctx.Done(), rate) - cancel() assert.False(t, underflow) example := goqueue.ExampleConvertSingle(item) assert.Equal(t, examples[i], example) @@ -447,6 +447,12 @@ func TestPeekFromHead(t *testing.T, newQueue func(int) interface { iExamples: []*goqueue.Example{{Int: 1}, {Int: 2}, {Int: 3}, {Int: 4}, {Int: 5}}, iPeekFromHead: 0, }, + "max-1": { + iSize: 5, + iExamples: []*goqueue.Example{{Int: 1}, {Int: 2}, {Int: 3}, {Int: 4}, {Int: 5}}, + iPeekFromHead: 4, + oPeekedExamples: []*goqueue.Example{{Int: 1}, {Int: 2}, {Int: 3}, {Int: 4}}, + }, "max+1": { iSize: 5, iExamples: []*goqueue.Example{{Int: 1}, {Int: 2}, {Int: 3}, {Int: 4}, {Int: 5}}, @@ -465,7 +471,7 @@ func TestPeekFromHead(t *testing.T, newQueue func(int) interface { assert.False(t, overflow, casef, cDesc) } values := goqueue.ExamplePeekFromHead(q, c.iPeekFromHead) - if assert.Equal(t, len(values), len(c.oPeekedExamples), casef, cDesc) { + if assert.Equal(t, len(c.oPeekedExamples), len(values), casef, cDesc) { for i, item := range values { assert.Equal(t, c.oPeekedExamples[i], item, casef, cDesc) } @@ -593,14 +599,14 @@ func TestLength(t *testing.T, newQueue func(int) interface { } } -//TestQueue -// 1. Use the New() function to create/populate a queue of the size for the case -// 2. Use the Length() function to verify that the queue is empty (size of 0) -// 3. Use the Enqueue() function for the number of itemsIn to place data in the queue and verify that -// the length increases by one each time. -// 4. Use the Length() to check to see if the queue is the expected size -// 5. Use the Dequeue() Function again to verify an underflow as the queue should now be empty (length of 0) -// 6. Use the Close() function to clean up all internal pointers for the queue +// TestQueue +// 1. Use the New() function to create/populate a queue of the size for the case +// 2. Use the Length() function to verify that the queue is empty (size of 0) +// 3. Use the Enqueue() function for the number of itemsIn to place data in the queue and verify that +// the length increases by one each time. +// 4. Use the Length() to check to see if the queue is the expected size +// 5. Use the Dequeue() Function again to verify an underflow as the queue should now be empty (length of 0) +// 6. Use the Close() function to clean up all internal pointers for the queue func TestQueue(t *testing.T, rate, timeout time.Duration, newQueue func(int) interface { goqueue.Owner goqueue.Enqueuer @@ -635,9 +641,10 @@ func TestQueue(t *testing.T, rate, timeout time.Duration, newQueue func(int) int for i := 0; i < len(c.iFloats); i++ { ctx, cancel := context.WithTimeout(context.TODO(), timeout) defer cancel() - value, underflow := goqueue.MustDequeue(q, ctx.Done(), rate) + item, underflow := goqueue.MustDequeue(q, ctx.Done(), rate) cancel() if assert.False(t, underflow, casef, cDesc) { + value := goqueue.ExampleConvertSingle(item) assert.Equal(t, c.iFloats[i], value, casef, cDesc) } } @@ -648,16 +655,16 @@ func TestQueue(t *testing.T, rate, timeout time.Duration, newQueue func(int) int } } -//TestAsync -// 1. Populate async interface using New() -// 2. Create two goRoutines: -// a. goRoutine (dequeue): -// (2) Stop when signal received after enqueue function is finished enqueing data -// (1) Constantly attempt to dequeue, when underflow is false, add item to slice of float64 -// b. goRoutine (enqueue): -// (1) Enqueue all the data from randFloats, store data in queue -// (2) Send signal when finished enqueuing data -// 3. Compare the items dequeued to the items enqueued, they should be equal although their quantity may not be the same (see verification) +// TestAsync +// 1. Populate async interface using New() +// 2. Create two goRoutines: +// a. goRoutine (dequeue): +// (2) Stop when signal received after enqueue function is finished enqueing data +// (1) Constantly attempt to dequeue, when underflow is false, add item to slice of float64 +// b. goRoutine (enqueue): +// (1) Enqueue all the data from randFloats, store data in queue +// (2) Send signal when finished enqueuing data +// 3. Compare the items dequeued to the items enqueued, they should be equal although their quantity may not be the same (see verification) func TestAsync(t *testing.T, newQueue func(int) interface { goqueue.Owner goqueue.Enqueuer diff --git a/version.json b/version.json index 4a6bc58..fae382e 100644 --- a/version.json +++ b/version.json @@ -1,3 +1,3 @@ { - "Version": "1.2.2" + "Version": "1.2.3" }