index_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package client
  17. import (
  18. "context"
  19. "fmt"
  20. "testing"
  21. "time"
  22. "github.com/stretchr/testify/mock"
  23. "github.com/stretchr/testify/suite"
  24. "go.uber.org/atomic"
  25. "github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
  26. "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
  27. "github.com/milvus-io/milvus/client/v2/entity"
  28. "github.com/milvus-io/milvus/client/v2/index"
  29. "github.com/milvus-io/milvus/pkg/util/merr"
  30. )
  31. type IndexSuite struct {
  32. MockSuiteBase
  33. }
  34. func (s *IndexSuite) TestCreateIndex() {
  35. ctx, cancel := context.WithCancel(context.Background())
  36. defer cancel()
  37. s.Run("success", func() {
  38. collectionName := fmt.Sprintf("coll_%s", s.randString(6))
  39. fieldName := fmt.Sprintf("field_%s", s.randString(4))
  40. indexName := fmt.Sprintf("idx_%s", s.randString(6))
  41. done := atomic.NewBool(false)
  42. s.mock.EXPECT().CreateIndex(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, cir *milvuspb.CreateIndexRequest) (*commonpb.Status, error) {
  43. s.Equal(collectionName, cir.GetCollectionName())
  44. s.Equal(fieldName, cir.GetFieldName())
  45. s.Equal(indexName, cir.GetIndexName())
  46. return merr.Success(), nil
  47. }).Once()
  48. s.mock.EXPECT().DescribeIndex(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, dir *milvuspb.DescribeIndexRequest) (*milvuspb.DescribeIndexResponse, error) {
  49. state := commonpb.IndexState_InProgress
  50. if done.Load() {
  51. state = commonpb.IndexState_Finished
  52. }
  53. return &milvuspb.DescribeIndexResponse{
  54. Status: merr.Success(),
  55. IndexDescriptions: []*milvuspb.IndexDescription{
  56. {
  57. FieldName: fieldName,
  58. IndexName: indexName,
  59. State: state,
  60. },
  61. },
  62. }, nil
  63. })
  64. defer s.mock.EXPECT().DescribeIndex(mock.Anything, mock.Anything).Unset()
  65. task, err := s.client.CreateIndex(ctx, NewCreateIndexOption(collectionName, fieldName, index.NewHNSWIndex(entity.L2, 32, 128)).WithIndexName(indexName))
  66. s.NoError(err)
  67. ch := make(chan struct{})
  68. go func() {
  69. defer close(ch)
  70. err := task.Await(ctx)
  71. s.NoError(err)
  72. }()
  73. select {
  74. case <-ch:
  75. s.FailNow("task done before index state set to finish")
  76. case <-time.After(time.Second):
  77. }
  78. done.Store(true)
  79. select {
  80. case <-ch:
  81. case <-time.After(time.Second):
  82. s.FailNow("task not done after index set finished")
  83. }
  84. })
  85. s.Run("failure", func() {
  86. collectionName := fmt.Sprintf("coll_%s", s.randString(6))
  87. fieldName := fmt.Sprintf("field_%s", s.randString(4))
  88. indexName := fmt.Sprintf("idx_%s", s.randString(6))
  89. s.mock.EXPECT().CreateIndex(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  90. _, err := s.client.CreateIndex(ctx, NewCreateIndexOption(collectionName, fieldName, index.NewHNSWIndex(entity.L2, 32, 128)).WithIndexName(indexName))
  91. s.Error(err)
  92. })
  93. }
  94. func (s *IndexSuite) TestListIndexes() {
  95. ctx, cancel := context.WithCancel(context.Background())
  96. defer cancel()
  97. s.Run("success", func() {
  98. collectionName := fmt.Sprintf("coll_%s", s.randString(6))
  99. s.mock.EXPECT().DescribeIndex(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, dir *milvuspb.DescribeIndexRequest) (*milvuspb.DescribeIndexResponse, error) {
  100. s.Equal(collectionName, dir.GetCollectionName())
  101. return &milvuspb.DescribeIndexResponse{
  102. Status: merr.Success(),
  103. IndexDescriptions: []*milvuspb.IndexDescription{
  104. {IndexName: "test_idx"},
  105. },
  106. }, nil
  107. }).Once()
  108. names, err := s.client.ListIndexes(ctx, NewListIndexOption(collectionName))
  109. s.NoError(err)
  110. s.ElementsMatch([]string{"test_idx"}, names)
  111. })
  112. s.Run("failure", func() {
  113. collectionName := fmt.Sprintf("coll_%s", s.randString(6))
  114. s.mock.EXPECT().DescribeIndex(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  115. _, err := s.client.ListIndexes(ctx, NewListIndexOption(collectionName))
  116. s.Error(err)
  117. })
  118. }
  119. func (s *IndexSuite) TestDescribeIndex() {
  120. ctx, cancel := context.WithCancel(context.Background())
  121. defer cancel()
  122. s.Run("success", func() {
  123. collectionName := fmt.Sprintf("coll_%s", s.randString(6))
  124. indexName := fmt.Sprintf("idx_%s", s.randString(6))
  125. s.mock.EXPECT().DescribeIndex(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, dir *milvuspb.DescribeIndexRequest) (*milvuspb.DescribeIndexResponse, error) {
  126. s.Equal(collectionName, dir.GetCollectionName())
  127. s.Equal(indexName, dir.GetIndexName())
  128. return &milvuspb.DescribeIndexResponse{
  129. Status: merr.Success(),
  130. IndexDescriptions: []*milvuspb.IndexDescription{
  131. {IndexName: indexName, Params: []*commonpb.KeyValuePair{
  132. {Key: index.IndexTypeKey, Value: string(index.HNSW)},
  133. }},
  134. },
  135. }, nil
  136. }).Once()
  137. index, err := s.client.DescribeIndex(ctx, NewDescribeIndexOption(collectionName, indexName))
  138. s.NoError(err)
  139. s.Equal(indexName, index.Name())
  140. })
  141. s.Run("no_index_found", func() {
  142. collectionName := fmt.Sprintf("coll_%s", s.randString(6))
  143. indexName := fmt.Sprintf("idx_%s", s.randString(6))
  144. s.mock.EXPECT().DescribeIndex(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, dir *milvuspb.DescribeIndexRequest) (*milvuspb.DescribeIndexResponse, error) {
  145. s.Equal(collectionName, dir.GetCollectionName())
  146. s.Equal(indexName, dir.GetIndexName())
  147. return &milvuspb.DescribeIndexResponse{
  148. Status: merr.Success(),
  149. IndexDescriptions: []*milvuspb.IndexDescription{},
  150. }, nil
  151. }).Once()
  152. _, err := s.client.DescribeIndex(ctx, NewDescribeIndexOption(collectionName, indexName))
  153. s.Error(err)
  154. s.ErrorIs(err, merr.ErrIndexNotFound)
  155. })
  156. s.Run("failure", func() {
  157. collectionName := fmt.Sprintf("coll_%s", s.randString(6))
  158. indexName := fmt.Sprintf("idx_%s", s.randString(6))
  159. s.mock.EXPECT().DescribeIndex(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  160. _, err := s.client.DescribeIndex(ctx, NewDescribeIndexOption(collectionName, indexName))
  161. s.Error(err)
  162. })
  163. }
  164. func (s *IndexSuite) TestDropIndexOption() {
  165. collectionName := fmt.Sprintf("coll_%s", s.randString(6))
  166. indexName := fmt.Sprintf("idx_%s", s.randString(6))
  167. opt := NewDropIndexOption(collectionName, indexName)
  168. req := opt.Request()
  169. s.Equal(collectionName, req.GetCollectionName())
  170. s.Equal(indexName, req.GetIndexName())
  171. }
  172. func (s *IndexSuite) TestDropIndex() {
  173. ctx, cancel := context.WithCancel(context.Background())
  174. defer cancel()
  175. s.Run("success", func() {
  176. s.mock.EXPECT().DropIndex(mock.Anything, mock.Anything).Return(merr.Success(), nil).Once()
  177. err := s.client.DropIndex(ctx, NewDropIndexOption("testCollection", "testIndex"))
  178. s.NoError(err)
  179. })
  180. s.Run("failure", func() {
  181. s.mock.EXPECT().DropIndex(mock.Anything, mock.Anything).Return(nil, merr.WrapErrServiceInternal("mocked")).Once()
  182. err := s.client.DropIndex(ctx, NewDropIndexOption("testCollection", "testIndex"))
  183. s.Error(err)
  184. })
  185. }
  186. func TestIndex(t *testing.T) {
  187. suite.Run(t, new(IndexSuite))
  188. }