From 86f98f3710a13199a7978d202d27c26d30d75159 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 22 Jul 2019 21:57:17 +0800 Subject: [PATCH] fix issue in gqueue in closing channel for limited queue --- g/container/gqueue/gqueue.go | 7 ++++-- g/database/gdb/gdb_unit_types_test.go | 35 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 g/database/gdb/gdb_unit_types_test.go diff --git a/g/container/gqueue/gqueue.go b/g/container/gqueue/gqueue.go index f51a633d7..369f86bf6 100644 --- a/g/container/gqueue/gqueue.go +++ b/g/container/gqueue/gqueue.go @@ -46,7 +46,7 @@ func New(limit ...int) *Queue { q := &Queue{ closed: gtype.NewBool(), } - if len(limit) > 0 { + if len(limit) > 0 && limit[0] > 0 { q.limit = limit[0] q.C = make(chan interface{}, limit[0]) } else { @@ -87,7 +87,7 @@ func (q *Queue) startAsyncLoop() { <-q.events } } - // It should be here to close q.C. + // It should be here to close q.C if is unlimited size. // It's the sender's responsibility to close channel when it should be closed. close(q.C) } @@ -119,6 +119,9 @@ func (q *Queue) Close() { if q.events != nil { close(q.events) } + if q.limit > 0 { + close(q.C) + } for i := 0; i < gDEFAULT_MAX_BATCH_SIZE; i++ { q.Pop() } diff --git a/g/database/gdb/gdb_unit_types_test.go b/g/database/gdb/gdb_unit_types_test.go new file mode 100644 index 000000000..65555c432 --- /dev/null +++ b/g/database/gdb/gdb_unit_types_test.go @@ -0,0 +1,35 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdb_test + +import ( + "fmt" + "testing" + + "github.com/gogf/gf/g/test/gtest" +) + +func Test_Types(t *testing.T) { + if _, err := db.Exec(fmt.Sprintf(` + CREATE TABLE types ( + id int(10) unsigned NOT NULL AUTO_INCREMENT, + blob blob NOT NULL, + binary binary(8) NOT NULL , + date varchar(45) NOT NULL , + decimal decimal(5,2) NOT NULL ', + double double NOT NULL ', + bit bit(2) NOT NULL ', + PRIMARY KEY (id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + `)); err != nil { + gtest.Error(err) + } + + gtest.Case(t, func() { + + }) +}