Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtbj committed Jun 13, 2019
1 parent 3e88462 commit 7aa683d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 66 deletions.
26 changes: 12 additions & 14 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion knative/helloworld.yaml → knative/picalc2.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: helloworld
name: picalc2
namespace: default
spec:
runLatest:
Expand Down
9 changes: 4 additions & 5 deletions src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ FROM golang:1.12 as builder

# Copy local code to the container image.
WORKDIR /go/src/github.com/bluebosh/knap-example
COPY helloworld.go helloworld_test.go .
COPY picalc2.go picalc2_test.go .

# Run unit tests
RUN CGO_ENABLED=0 GOOS=linux go test

# Build the code inside the container.
RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld
RUN CGO_ENABLED=0 GOOS=linux go build -v -o picalc2

# Use a Docker multi-stage build to create a lean production image.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine

# Copy the binary to the production image from the builder stage.
COPY --from=builder /go/src/github.com/bluebosh/knap-example /helloworld
RUN chmod a+x /helloworld/*
COPY --from=builder /go/src/github.com/bluebosh/knap-example /picalc2

# Service must listen to $PORT environment variable.
# This default value facilitates local development.
ENV PORT 8080

# Run the web service on container startup.
CMD ["/helloworld"]
CMD ["/picalc2"]
33 changes: 0 additions & 33 deletions src/helloworld.go

This file was deleted.

13 changes: 0 additions & 13 deletions src/helloworld_test.go

This file was deleted.

45 changes: 45 additions & 0 deletions src/picalc2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
"strconv"
)

// Calculate pi using Gregory-Leibniz series: (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15) ...
func calculatePi(iterations int) float64 {
var result float64 = 0.0
var sign float64 = 1.0
var denominator float64 = 1.0
for i := 0; i < iterations; i++ {
result = result + (sign * 4/denominator)
denominator = denominator + 2
sign = -sign
}
return result
}

func handler(w http.ResponseWriter, r *http.Request) {
log.Print("Pi calculator received a request.")
iterations, err := strconv.Atoi(r.URL.Query()["iterations"][0])
if err != nil {
fmt.Fprintf(w, "iterations parameter not valid\n")
return
}
fmt.Fprintf(w, "%.10f\n", calculatePi(iterations))
}

func main() {
log.Print("Pi calculator started.")

http.HandleFunc("/", handler)

port := os.Getenv("PORT")
if port == "" {
port = "8080"
}

log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
11 changes: 11 additions & 0 deletions src/picalc2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import "testing"

func TestCalculatePi(t *testing.T) {
var pi float64
pi = calculatePi(20000000)
if pi < 3.1415926 || pi >= 3.1415927 {
t.Errorf("Value %.10f is incorrect", pi)
}
}

0 comments on commit 7aa683d

Please sign in to comment.