Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
Signed-off-by: mudler <[email protected]>
  • Loading branch information
mudler committed Oct 28, 2024
1 parent 142bdb0 commit 6dbd802
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 2 deletions.
13 changes: 13 additions & 0 deletions internal/jobserver/jobserver_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jobserver_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestJobServer(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "JobServer test suite")
}
39 changes: 39 additions & 0 deletions internal/jobserver/jobserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package jobserver_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/masa-finance/tee-worker/api/types"
. "github.com/masa-finance/tee-worker/internal/jobserver"
)

var _ = Describe("Jobserver", func() {
It("runs jobs", func() {
jobserver := NewJobServer(2)

uuid := jobserver.AddJob(types.Job{
Type: "web-scraper",
Arguments: map[string]interface{}{
"url": "google",
},
})

Expect(uuid).ToNot(BeEmpty())

_, exists := jobserver.GetJobResult(uuid)
Expect(exists).ToNot(BeTrue())

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go jobserver.Run(ctx)

Eventually(func() bool {
result, exists := jobserver.GetJobResult(uuid)
return exists && result.Error == "" && result.Data.(string) == "google"
}, "5s").Should(Not(BeNil()))
})
})
3 changes: 1 addition & 2 deletions pkg/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"

Expand Down Expand Up @@ -105,7 +104,7 @@ func (c *Client) DecryptResult(encryptedResult string) (string, error) {
return "", fmt.Errorf("error: received status code %d from /decrypt", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body from /decrypt: %w", err)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/client/http_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package client_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Client test suite")
}
140 changes: 140 additions & 0 deletions pkg/client/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package client_test

import (
"encoding/json"
"net/http"
"net/http/httptest"
"time"

"github.com/masa-finance/tee-worker/pkg/client"
. "github.com/masa-finance/tee-worker/pkg/client"

"github.com/masa-finance/tee-worker/api/types"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Client", func() {
var (
c *Client
server *httptest.Server
mockJobUID string
)

// Define a helper function to create a new test server
setupServer := func(statusCode int, responseBody interface{}) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(statusCode)
if responseBody != nil {
json.NewEncoder(w).Encode(responseBody)
}
}))
}

BeforeEach(func() {
mockJobUID = "mock-job-uid"
})

Context("SubmitJob", func() {
var mockJob types.Job

BeforeEach(func() {
mockJob = types.Job{UUID: "job1"}
server = setupServer(http.StatusOK, types.JobResponse{UID: mockJobUID})
c = client.NewClient(server.URL)
})

AfterEach(func() {
server.Close()
})

It("should submit a job successfully", func() {
uid, err := c.SubmitJob(mockJob)
Expect(err).NotTo(HaveOccurred())
Expect(uid).To(Equal(mockJobUID))
})

It("should return an error on HTTP failure", func() {
server.Close() // close the server to simulate network error
_, err := c.SubmitJob(mockJob)
Expect(err).To(HaveOccurred())
})
})

Context("GetJobResult", func() {
BeforeEach(func() {
server = setupServer(http.StatusOK, "encrypted-result")
c = client.NewClient(server.URL)
})

AfterEach(func() {
server.Close()
})

It("should retrieve the job result successfully", func() {
result, err := c.GetJobResult("job1")
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal("\"encrypted-result\"\n"))
})

It("should return an error when job is not found", func() {
server.Close()
server = setupServer(http.StatusNotFound, types.JobError{Error: "job not found"})
c = client.NewClient(server.URL)
_, err := c.GetJobResult("invalid-job-id")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("job not found"))
})
})

Context("DecryptResult", func() {
BeforeEach(func() {
server = setupServer(http.StatusOK, "decrypted-result")
c = client.NewClient(server.URL)
})

AfterEach(func() {
server.Close()
})

It("should decrypt result successfully", func() {
result, err := c.DecryptResult("encrypted-result")
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal("\"decrypted-result\"\n"))
})

It("should return an error on decryption failure", func() {
server.Close()
server = setupServer(http.StatusInternalServerError, nil)
c = client.NewClient(server.URL)
_, err := c.DecryptResult("encrypted-result")
Expect(err).To(HaveOccurred())
})
})

Context("WaitForResult", func() {
BeforeEach(func() {
server = setupServer(http.StatusOK, "encrypted-result")
c = client.NewClient(server.URL)
})

AfterEach(func() {
server.Close()
})

It("should wait and retrieve result successfully", func() {
result, err := c.WaitForResult("job1", 3, time.Millisecond*10)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal("\"encrypted-result\"\n"))
})

It("should fail after max retries", func() {
server.Close() // simulate unavailability
result, err := c.WaitForResult("job1", 3, time.Millisecond*10)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("max retries reached"))
Expect(result).To(BeEmpty())
})
})
})

0 comments on commit 6dbd802

Please sign in to comment.