Browse Source

SVI Доработка тестов; 77.8%

user 9 months ago
parent
commit
f6728b46b2
4 changed files with 42 additions and 4 deletions
  1. 2 0
      pkg/mock/mock.go
  2. 30 2
      pkg/mock/mock_test.go
  3. 5 0
      pkg/serv_http/serv_http.go
  4. 5 2
      pkg/store/store.go

+ 2 - 0
pkg/mock/mock.go

@@ -22,6 +22,8 @@ func MakeMock() (types.IService, error) {
 	}
 	serv.ServHttp_, err = serv_http.NewServHttp(serv)
 	if err != nil {
+		serv.CancelApp()
+		serv.Wg().Wait()
 		return nil, fmt.Errorf("MakeMock(): in create ServHttp, err=\n\t%w", err)
 	}
 	return serv, err

+ 30 - 2
pkg/mock/mock_test.go

@@ -4,6 +4,7 @@ import (
 	"os"
 	"testing"
 
+	"git.p78su.freemyip.com/svi/gostore/pkg/serv_http"
 	"git.p78su.freemyip.com/svi/gostore/pkg/store"
 )
 
@@ -27,6 +28,33 @@ func TestMock(t *testing.T) {
 func (sf *tester) create() {
 	sf.t.Log("create")
 	sf.createBad1()
+	sf.createBad2()
+	sf.createGood1()
+}
+
+func (sf *tester) createGood1() {
+	sf.t.Log("createGood1")
+	serv, err := MakeMock()
+	if err != nil {
+		sf.t.Fatalf("createGood1(): err = %v", err)
+	}
+	if serv == nil {
+		sf.t.Fatalf("createGood1(): serv == nil")
+	}
+}
+
+// Ошибка при создании HTTP-сервера
+func (sf *tester) createBad2() {
+	sf.t.Log("createBad2")
+	serv_http.IsBad_ = true
+	serv, err := MakeMock()
+	if err == nil {
+		sf.t.Fatalf("createBad2(): err == nil")
+	}
+	if serv != nil {
+		sf.t.Fatalf("createBad2(): serv != nil")
+	}
+	serv_http.IsBad_ = false
 }
 
 // Ошибка при создании хранилища
@@ -35,10 +63,10 @@ func (sf *tester) createBad1() {
 	store.IsBad_ = true
 	serv, err := MakeMock()
 	if err == nil {
-		sf.t.Error("createBad1(): err == nil")
+		sf.t.Fatalf("createBad1(): err == nil")
 	}
 	if serv != nil {
-		sf.t.Error("createBad1(): serv != nil")
+		sf.t.Fatalf("createBad1(): serv != nil")
 	}
 	store.IsBad_ = false
 }

+ 5 - 0
pkg/serv_http/serv_http.go

@@ -29,8 +29,13 @@ type ServHttp struct {
 	isWork   bool // Признак работы
 }
 
+var IsBad_ bool // Для тестов
+
 // NewServHttp -- возвращает новый HTTP-сервер
 func NewServHttp(serv types.IService) (types.IServHttp, error) {
+	if IsBad_ {
+		return nil, fmt.Errorf("NewServHttp(): IsBad_==true")
+	}
 	if serv == nil {
 		return nil, fmt.Errorf("NewServHttp(): IService==nil")
 	}

+ 5 - 2
pkg/store/store.go

@@ -32,13 +32,16 @@ func NewStore(serv types.IServCtx) (types.IStore, error) {
 	if serv == nil {
 		return nil, fmt.Errorf("NewStore(): IServCtx==nil")
 	}
-	_ = os.MkdirAll("./store/db", 0750)
+	err := os.MkdirAll("./store", 0750)
+	if err != nil {
+		return nil, fmt.Errorf("NewStore(): in create dir, err=\n\t%w", err)
+	}
 	db, err := leveldb.OpenFile("./store/db", nil)
 	if err != nil {
 		if !strings.Contains(err.Error(), "leveldb: manifest corrupted") {
 			return nil, fmt.Errorf("NewStore(): in create IStoreDisk, err=\n\t%w", err)
 		}
-		db, err = leveldb.RecoverFile("./stor/db", nil)
+		db, err = leveldb.RecoverFile("./store/db", nil)
 		if err != nil {
 			return nil, fmt.Errorf("NewStore(): in recovery DB, err=\n\t%w", err)
 		}