From fd8a3f2b24ad1655d79b631e71b7cc74ee08208c Mon Sep 17 00:00:00 2001
From: eson <474420502@qq.com>
Date: Wed, 28 Apr 2021 17:26:09 +0800
Subject: [PATCH] feat(pushquque): finish the test that push queue

---
 server/go.mod       |  3 ++-
 server/go.sum       | 19 ++++++++++++++++++-
 server/main.go      | 26 +++++++++++++++++---------
 server/queue.go     |  7 ++++++-
 server/task.go      | 42 ++++++++++++++++++++++++++++++++++++++++--
 server/task_test.go | 22 ++++++++++++++++++++++
 6 files changed, 105 insertions(+), 14 deletions(-)

diff --git a/server/go.mod b/server/go.mod
index 0cf49ef..f62d4fa 100644
--- a/server/go.mod
+++ b/server/go.mod
@@ -3,7 +3,8 @@ module server
 go 1.16
 
 require (
-	github.com/474420502/focus v0.13.3 // indirect
+	github.com/474420502/focus v0.13.3
+	github.com/474420502/requests v1.11.0
 	github.com/gin-gonic/gin v1.7.1
 	github.com/gorilla/websocket v1.4.2
 )
diff --git a/server/go.sum b/server/go.sum
index 6fd5983..7e0e0ed 100644
--- a/server/go.sum
+++ b/server/go.sum
@@ -1,11 +1,17 @@
 github.com/474420502/focus v0.13.3 h1:fiP6HNSfOukx76xUovKG5WzswMysHF7A5xhyrcYCrLU=
 github.com/474420502/focus v0.13.3/go.mod h1:SrqNq63qSx53TkvVWOuHTbTSvAQTNO8ZUrufXm0Ncq8=
+github.com/474420502/requests v1.11.0 h1:YOU9Aq6gzJVQFnHxH3bXBl1zddMjxOGtA9oOxPv2/KA=
+github.com/474420502/requests v1.11.0/go.mod h1:SB8/RIUVWF3AGotuq/mATUwAjXzzlV7iWaBNM3+c06c=
 github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2 h1:aZtFdDNWY/yH86JPR2WX/PN63635VsE/f/nXNPAbYxY=
+github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
+github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
 github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
 github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
+github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
 github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8=
 github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
 github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
@@ -14,6 +20,7 @@ github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8c
 github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
 github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
 github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
+github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
 github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
 github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
 github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
@@ -33,10 +40,17 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD
 github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
+github.com/tidwall/gjson v1.3.2 h1:+7p3qQFaH3fOMXAJSrdZwGKcOO/lYdGS0HqGhPqDdTI=
+github.com/tidwall/gjson v1.3.2/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
+github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
+github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
+github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
+github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
 github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
 github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
 github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
@@ -45,10 +59,13 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
+golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
 golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
+golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
 golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
diff --git a/server/main.go b/server/main.go
index e99a5b9..5206e1b 100644
--- a/server/main.go
+++ b/server/main.go
@@ -4,7 +4,6 @@ import (
 	"encoding/json"
 	"log"
 	"net/http"
-	"time"
 
 	"github.com/gin-gonic/gin"
 	"github.com/gorilla/websocket"
@@ -16,6 +15,12 @@ var upGrader = websocket.Upgrader{
 	},
 }
 
+var RemoteAddress = ":12203"
+
+func init() {
+	log.SetFlags(log.Llongfile)
+}
+
 func main() {
 	eg := gin.Default()
 	eg.Use(Cors())
@@ -26,6 +31,8 @@ func main() {
 		})
 	})
 
+	eg.POST("queue", PostPushQueue)
+
 	eg.GET("task", func(c *gin.Context) {
 		//升级get请求为webSocket协议
 		ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
@@ -40,6 +47,8 @@ func main() {
 			return
 		}
 
+		log.Println(string(message))
+
 		for {
 
 			//读取ws中的数据
@@ -47,20 +56,19 @@ func main() {
 			log.Println(
 				"123",
 			)
-			msg := gin.H{
-				"content": string(message),
-			}
-			data, err := json.Marshal(msg)
+
+			task := taskQueue.PopBlock()
+			taskData, err := json.Marshal(task)
 			if err != nil {
-				log.Println(err)
+				log.Println(err, task)
+				continue
 			}
 			//写入ws数据
-			err = ws.WriteMessage(mt, data)
+			err = ws.WriteMessage(mt, taskData)
 			if err != nil {
-				c.Error(err)
+				log.Println(err)
 				break
 			}
-			time.Sleep(time.Second * 10)
 		}
 
 	})
diff --git a/server/queue.go b/server/queue.go
index 29af6d3..82dfc3b 100644
--- a/server/queue.go
+++ b/server/queue.go
@@ -3,13 +3,18 @@ package main
 import (
 	"container/list"
 	"sync"
+	"time"
 
 	"github.com/474420502/focus/compare"
 	pqueuekey "github.com/474420502/focus/priority_queuekey"
 )
 
 type Task struct {
-	Value int
+	Name     string      `json:"name"`      // 任务名字
+	Type     int64       `json:"type"`      // 类型
+	Priority int64       `json:"priority"`  // 优先级 默认0
+	CreateAt time.Time   `json:"create_at"` // 创建任务时间
+	Info     interface{} `json:"info"`      // 任务的信息内容
 }
 
 type Queue struct {
diff --git a/server/task.go b/server/task.go
index 4404f42..965f71e 100644
--- a/server/task.go
+++ b/server/task.go
@@ -1,7 +1,45 @@
 package main
 
-import "github.com/gin-gonic/gin"
+import (
+	"log"
+	"net/http"
+	"runtime/debug"
 
-func PushQueue(c *gin.Context) {
+	"github.com/gin-gonic/gin"
+)
 
+type Response struct {
+	Code    int    `json:"code"`
+	Message string `json:"message"`
+	Content string `json:"content"`
+}
+
+func responseError(c *gin.Context, err error) {
+	log.Println(err)
+	debug.PrintStack()
+	c.JSON(http.StatusInternalServerError, Response{
+		Code:    http.StatusInternalServerError,
+		Message: err.Error(),
+	})
+}
+
+func responseOk(c *gin.Context) {
+	c.JSON(http.StatusOK, Response{
+		Code:    http.StatusOK,
+		Message: "ok",
+	})
+}
+
+var taskQueue = NewQueue()
+
+func PostPushQueue(c *gin.Context) {
+	// stask := c.PostForm("task")
+	task := &Task{}
+	err := c.BindJSON(task)
+	if err != nil {
+		responseError(c, err)
+		return
+	}
+	taskQueue.Push(task)
+	responseOk(c)
 }
diff --git a/server/task_test.go b/server/task_test.go
index bd0d734..22888f5 100644
--- a/server/task_test.go
+++ b/server/task_test.go
@@ -1,9 +1,13 @@
 package main
 
 import (
+	"log"
 	"sync"
 	"testing"
 	"time"
+
+	"github.com/474420502/requests"
+	"github.com/gin-gonic/gin"
 )
 
 func TestTaskCasePushPop(t *testing.T) {
@@ -65,3 +69,21 @@ func TestTaskCaseBlock(t *testing.T) {
 
 	wait.Wait()
 }
+
+func TestTaskPostPushQueue(t *testing.T) {
+	temp := requests.NewSession().Post("http://localhost" + RemoteAddress + "/queue")
+	task := &Task{
+		Name:     "test",
+		Type:     1,
+		CreateAt: time.Now(),
+		Info: gin.H{
+			"data": "test",
+		},
+	}
+	temp.SetBodyAuto(task)
+	resp, err := temp.Execute()
+	if err != nil {
+		t.Error(err)
+	}
+	log.Println(string(resp.Content()))
+}