func TestSchemaInfoDropTable(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoTestSupportedQueries() { db.AddQuery(query, result) } existingTable := "test_table_01" createOrDropTableQuery := fmt.Sprintf("%s and table_name = '%s'", baseShowTables, existingTable) db.AddQuery(createOrDropTableQuery, &mproto.QueryResult{ RowsAffected: 1, Rows: [][]sqltypes.Value{ createTestTableBaseShowTable(existingTable), }, }) schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false) appParams := sqldb.ConnParams{} dbaParams := sqldb.ConnParams{} cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats) cachePool.Open() defer cachePool.Close() schemaInfo.Open(&appParams, &dbaParams, getSchemaInfoTestSchemaOverride(), cachePool, false) tableInfo := schemaInfo.GetTable(existingTable) if tableInfo == nil { t.Fatalf("table: %s should exist", existingTable) } schemaInfo.DropTable(existingTable) tableInfo = schemaInfo.GetTable(existingTable) if tableInfo != nil { t.Fatalf("table: %s should not exist", existingTable) } schemaInfo.Close() }
func TestCachePoolState(t *testing.T) { fakecacheservice.Register() fakesqldb.Register() rowCacheConfig := RowCacheConfig{ Binary: "ls", Connections: 100, } cachePool := newTestCachePool(rowCacheConfig, true) idleTimeout := 1 * time.Second cachePool.idleTimeout = idleTimeout cachePool.Open() cachePool.memcacheStats.update() defer cachePool.Close() if cachePool.Available() <= 0 { t.Fatalf("cache pool should have connections available") } if cachePool.Capacity() <= 0 { t.Fatalf("cache pool should have positive capacity") } if cachePool.MaxCap() <= 0 { t.Fatalf("cache pool should have positive max cap") } if cachePool.WaitCount() > 0 { t.Fatalf("cache pool has never waited for a connection, WaitCount should return 0") } if cachePool.WaitTime() > 0 { t.Fatalf("cache pool has never waited for a connection, WaitTime should return 0") } if cachePool.IdleTimeout() != idleTimeout { t.Fatalf("cache pool's idle timeout does not match the specified one") } if len(cachePool.StatsJSON()) <= 0 { t.Fatalf("cache pool stats json should return non empty result") } }
func TestCachePoolStateWithoutOpen(t *testing.T) { fakecacheservice.Register() fakesqldb.Register() rowCacheConfig := RowCacheConfig{ Binary: "ls", Connections: 100, } cachePool := newTestCachePool(rowCacheConfig, false) idleTimeout := 1 * time.Second cachePool.idleTimeout = idleTimeout if cachePool.StatsJSON() != "{}" { t.Fatalf("cache pool StatsJSON() should return {}") } if cachePool.Capacity() != 0 { t.Fatalf("cache pool Capacity() should return 0") } if cachePool.Available() != 0 { t.Fatalf("cache pool Available() should return 0") } if cachePool.MaxCap() != 0 { t.Fatalf("cache pool MaxCap() should return 0") } if cachePool.WaitCount() != 0 { t.Fatalf("cache pool WaitCount() should return 0") } if cachePool.WaitTime() != 0 { t.Fatalf("cache pool WaitTime() should return 0") } if cachePool.IdleTimeout() != 0 { t.Fatalf("cache pool IdleTimeout() should return 0") } cachePool.Put(nil) }
func TestTableInfoWithoutRowCacheViaNoPKColumn(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() db.AddQuery("show index from `test_table`", &sqltypes.Result{}) db.AddQuery("select * from `test_table` where 1 != 1", &sqltypes.Result{ Fields: []*querypb.Field{{ Name: "pk", Type: sqltypes.Int32, }}, }) db.AddQuery("describe `test_table`", &sqltypes.Result{ RowsAffected: 1, Rows: [][]sqltypes.Value{ { sqltypes.MakeString([]byte("pk")), sqltypes.MakeString([]byte("int")), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte("1")), sqltypes.MakeString([]byte{}), }, }, }) cachePool := newTestTableInfoCachePool() cachePool.Open() defer cachePool.Close() tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db) if err != nil { t.Fatalf("failed to create a test table info") } if tableInfo.Cache != nil { t.Fatalf("table info's rowcache should be disabled") } }
func TestSchemaInfoCreateOrUpdateTableFailedDuetoExecErr(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoTestSupportedQueries() { db.AddQuery(query, result) } createOrDropTableQuery := fmt.Sprintf("%s and table_name = '%s'", baseShowTables, "test_table") db.AddQuery(createOrDropTableQuery, &mproto.QueryResult{ // make this query fail RowsAffected: math.MaxUint64, Rows: [][]sqltypes.Value{ createTestTableBaseShowTable("test_table"), }, }) schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false) appParams := sqldb.ConnParams{} dbaParams := sqldb.ConnParams{} cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats) cachePool.Open() defer cachePool.Close() defer handleAndVerifyTabletError( t, "CreateOrUpdateTable should fail because it could not tables from MySQL", ErrFail, ) schemaInfo.Open(&appParams, &dbaParams, getSchemaInfoTestSchemaOverride(), cachePool, false) defer schemaInfo.Close() schemaInfo.CreateOrUpdateTable(context.Background(), "test_table") }
func TestTableInfoInvalidCardinalityInIndex(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getTestTableInfoQueries() { db.AddQuery(query, result) } db.AddQuery("show index from `test_table`", &sqltypes.Result{ RowsAffected: 1, Rows: [][]sqltypes.Value{ { sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte("PRIMARY")), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte("pk")), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte("invalid")), }, }, }) cachePool := newTestTableInfoCachePool() cachePool.Open() defer cachePool.Close() tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db) if err != nil { t.Fatalf("failed to create a table info: %v", err) } if len(tableInfo.PKColumns) != 1 { t.Fatalf("table should have one PK column although the cardinality is invalid") } }
func TestSchemaInfoOpenFailedDueToTableInfoErr(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoBaseTestQueries() { db.AddQuery(query, result) } db.AddQuery(baseShowTables, &mproto.QueryResult{ RowsAffected: 1, Rows: [][]sqltypes.Value{ createTestTableBaseShowTable("test_table"), }, }) db.AddQuery("describe `test_table`", &mproto.QueryResult{ // this will cause NewTableInfo error RowsAffected: math.MaxUint64, }) schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false) appParams := sqldb.ConnParams{} dbaParams := sqldb.ConnParams{} cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats) cachePool.Open() defer cachePool.Close() defer handleAndVerifyTabletError( t, "schema info Open should fail because NewTableInfo failed", ErrFail, ) schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, cachePool, false) }
func TestCachePoolStatsURL(t *testing.T) { cache := fakecacheservice.Register() fakesqldb.Register() rowCacheConfig := RowCacheConfig{ Binary: "ls", Connections: 100, } cachePool := newTestCachePool(rowCacheConfig, false) idleTimeout := 1 * time.Second cachePool.idleTimeout = idleTimeout cachePool.Open() request, _ := http.NewRequest("GET", fmt.Sprintf("%sstats", cachePool.statsURL), nil) response := httptest.NewRecorder() cachePool.ServeHTTP(response, request) // any memcache calls should fail cache.EnableCacheServiceError() response = httptest.NewRecorder() cachePool.ServeHTTP(response, request) cache.DisableCacheServiceError() cachePool.Close() response = httptest.NewRecorder() cachePool.ServeHTTP(response, request) body, _ := ioutil.ReadAll(response.Body) matcher := regexp.MustCompile("closed") if !matcher.Match(body) { t.Fatalf("stats page should contain 'closed', but got %s", string(body)) } }
func TestSchemaInfoCreateOrUpdateTableFailedDuetoExecErr(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoTestSupportedQueries() { db.AddQuery(query, result) } createOrDropTableQuery := fmt.Sprintf("%s and table_name = '%s'", baseShowTables, "test_table") db.AddQuery(createOrDropTableQuery, &sqltypes.Result{ // make this query fail RowsAffected: math.MaxUint64, Rows: [][]sqltypes.Value{ createTestTableBaseShowTable("test_table"), }, }) schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, true) appParams := sqldb.ConnParams{Engine: db.Name} dbaParams := sqldb.ConnParams{Engine: db.Name} schemaInfo.cachePool.Open() defer schemaInfo.cachePool.Close() schemaInfo.Open(&appParams, &dbaParams, getSchemaInfoTestSchemaOverride(), false) defer schemaInfo.Close() originalSchemaErrorCount := schemaInfo.queryServiceStats.InternalErrors.Counts()["Schema"] // should silently fail: no errors returned, but increment a counter schemaInfo.CreateOrUpdateTable(context.Background(), "test_table") newSchemaErrorCount := schemaInfo.queryServiceStats.InternalErrors.Counts()["Schema"] schemaErrorDiff := newSchemaErrorCount - originalSchemaErrorCount if schemaErrorDiff != 1 { t.Errorf("InternalErrors.Schema counter should have increased by 1, instead got %v", schemaErrorDiff) } }
func TestSchemaInfoGetPlanPanicDuetoEmptyQuery(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoTestSupportedQueries() { db.AddQuery(query, result) } schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, false) appParams := sqldb.ConnParams{} dbaParams := sqldb.ConnParams{} cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats) cachePool.Open() defer cachePool.Close() schemaOverrides := getSchemaInfoTestSchemaOverride() // test cache type RW schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, cachePool, true) defer schemaInfo.Close() ctx := context.Background() logStats := newLogStats("GetPlanStats", ctx) defer handleAndVerifyTabletError( t, "schema info GetPlan should fail because of empty query", ErrFail, ) schemaInfo.GetPlan(ctx, logStats, "") }
func TestSchemaInfoOpenWithSchemaOverride(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoTestSupportedQueries() { db.AddQuery(query, result) } schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, false) appParams := sqldb.ConnParams{} dbaParams := sqldb.ConnParams{} cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats) cachePool.Open() defer cachePool.Close() schemaOverrides := getSchemaInfoTestSchemaOverride() // test cache type RW schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, cachePool, true) testTableInfo := schemaInfo.GetTable("test_table_01") if testTableInfo.Table.CacheType != schema.CACHE_RW { t.Fatalf("test_table_01's cache type should be RW") } schemaInfo.Close() // test cache type W schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, cachePool, true) testTableInfo = schemaInfo.GetTable("test_table_02") if testTableInfo.Table.CacheType != schema.CACHE_W { t.Fatalf("test_table_02's cache type should be W") } schemaInfo.Close() }
func TestTableInfoFailBecauseUnableToRetrieveTableIndex(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getTestTableInfoQueries() { db.AddQuery(query, result) } db.AddRejectedQuery("show index from `test_table`", errRejected) cachePool := newTestTableInfoCachePool() cachePool.Open() defer cachePool.Close() _, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db) if err == nil { t.Fatalf("table info creation should fail because it is unable to get test_table index") } }
func TestTableInfoWithoutRowCacheViaUnknownPKColumnType(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() db.AddQuery("show index from `test_table`", &mproto.QueryResult{ RowsAffected: 1, Rows: [][]sqltypes.Value{ []sqltypes.Value{ sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte("PRIMARY")), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte("pk")), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte("300")), }, }, }) db.AddQuery("select * from `test_table` where 1 != 1", &mproto.QueryResult{ Fields: []mproto.Field{{ Name: "pk", Type: mysql.TypeNewDecimal, }}, }) db.AddQuery("describe `test_table`", &mproto.QueryResult{ RowsAffected: 1, Rows: [][]sqltypes.Value{ []sqltypes.Value{ sqltypes.MakeString([]byte("pk")), sqltypes.MakeString([]byte("decimal")), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte{}), sqltypes.MakeString([]byte("1")), sqltypes.MakeString([]byte{}), }, }, }) cachePool := newTestTableInfoCachePool() cachePool.Open() defer cachePool.Close() tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db) if err != nil { t.Fatalf("failed to create a test table info") } if tableInfo.Cache != nil { t.Fatalf("table info's rowcache should be disabled") } }
func TestCachePoolOpenWithInvalidBinary(t *testing.T) { fakecacheservice.Register() fakesqldb.Register() rowCacheConfig := RowCacheConfig{ Binary: "invalid_binary", Connections: 100, } cachePool := newTestCachePool(rowCacheConfig, false) defer func() { if e := recover(); e == nil { t.Fatalf("open a cache pool with an invalid rowCacheConfig.Binary should panic") } }() cachePool.Open() cachePool.Close() }
func TestCachePoolFailToStartBecauseCacheServiceWasDown(t *testing.T) { cache := fakecacheservice.Register() fakesqldb.Register() testUtils := &testUtils{} rowCacheConfig := RowCacheConfig{ Binary: "ls", Connections: 100, } cachePool := newTestCachePool(rowCacheConfig, false) idleTimeout := 1 * time.Second cachePool.idleTimeout = idleTimeout // any memcache calls should fail cache.EnableCacheServiceError() defer testUtils.checkTabletErrorWithRecover(t, ErrFatal, "can't communicate with cache service") cachePool.Open() }
func TestCachePoolOpenTwice(t *testing.T) { fakecacheservice.Register() fakesqldb.Register() rowCacheConfig := RowCacheConfig{ Binary: "ls", Connections: 100, } cachePool := newTestCachePool(rowCacheConfig, false) cachePool.Open() defer cachePool.Close() defer func() { if e := recover(); e == nil { t.Fatalf("open an opened cache pool should panic") } }() cachePool.Open() }
func TestSchemaInfoExportVars(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoTestSupportedQueries() { db.AddQuery(query, result) } schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, true) appParams := sqldb.ConnParams{Engine: db.Name} dbaParams := sqldb.ConnParams{Engine: db.Name} schemaInfo.cachePool.Open() defer schemaInfo.cachePool.Close() schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, true) defer schemaInfo.Close() expvar.Do(func(kv expvar.KeyValue) { _ = kv.Value.String() }) }
func TestTableInfoWithoutRowCacheViaTableType(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getTestTableInfoQueries() { db.AddQuery(query, result) } cachePool := newTestTableInfoCachePool() cachePool.Open() defer cachePool.Close() tableInfo, err := newTestTableInfo(cachePool, "VIEW", "test table", db) if err != nil { t.Fatalf("failed to create a test table info") } if tableInfo.Cache != nil { t.Fatalf("table info's rowcache should be disabled") } }
func TestCachePoolGetFailedBecauseCachePoolIsClosed(t *testing.T) { fakecacheservice.Register() fakesqldb.Register() rowCacheConfig := RowCacheConfig{ Binary: "ls", Connections: 100, } cachePool := newTestCachePool(rowCacheConfig, false) idleTimeout := 1 * time.Second cachePool.idleTimeout = idleTimeout ctx := context.Background() defer func() { if err := recover(); err == nil { t.Fatalf("Get should fail because cache pool is closed") } }() cachePool.Get(ctx) }
func TestCachePool(t *testing.T) { fakecacheservice.Register() fakesqldb.Register() rowCacheConfig := RowCacheConfig{ Binary: "ls", Connections: 100, } cachePool := newTestCachePool(rowCacheConfig, false) if !cachePool.IsClosed() { t.Fatalf("cache pool is not closed") } cachePool.Open() if cachePool.IsClosed() { t.Fatalf("cache pool is closed") } cachePool.Close() if !cachePool.IsClosed() { t.Fatalf("cache pool is not closed") } }
func TestTableInfoWithoutRowCacheViaComment(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getTestTableInfoQueries() { db.AddQuery(query, result) } cachePool := newTestTableInfoCachePool() cachePool.Open() defer cachePool.Close() tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "vitess_nocache", db) if err != nil { t.Fatalf("failed to create a test table info") } if tableInfo.Cache != nil { t.Fatalf("table info's rowcache should be disabled") } if tableInfo.StatsJSON() != "null" { t.Fatalf("rowcache is disabled, stats should be null") } }
func TestSchemaInfoStrictMode(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoBaseTestQueries() { db.AddQuery(query, result) } schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false) appParams := sqldb.ConnParams{} dbaParams := sqldb.ConnParams{} cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats) cachePool.Open() defer cachePool.Close() defer handleAndVerifyTabletError( t, "schema info Open should fail because of underlying "+ "connection cannot verify strict mode", ErrFatal, ) schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, cachePool, true) }
func TestSchemaInfoStatsURL(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoTestSupportedQueries() { db.AddQuery(query, result) } query := "select * from test_table_01" db.AddQuery("select * from test_table_01 where 1 != 1", &mproto.QueryResult{}) schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false) appParams := sqldb.ConnParams{} dbaParams := sqldb.ConnParams{} cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats) cachePool.Open() defer cachePool.Close() schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, cachePool, true) defer schemaInfo.Close() // warm up cache ctx := context.Background() logStats := newLogStats("GetPlanStats", ctx) schemaInfo.GetPlan(ctx, logStats, query) request, _ := http.NewRequest("GET", schemaInfo.endpoints[debugQueryPlansKey], nil) response := httptest.NewRecorder() schemaInfo.ServeHTTP(response, request) request, _ = http.NewRequest("GET", schemaInfo.endpoints[debugQueryStatsKey], nil) response = httptest.NewRecorder() schemaInfo.ServeHTTP(response, request) request, _ = http.NewRequest("GET", schemaInfo.endpoints[debugTableStatsKey], nil) response = httptest.NewRecorder() schemaInfo.ServeHTTP(response, request) request, _ = http.NewRequest("GET", schemaInfo.endpoints[debugSchemaKey], nil) response = httptest.NewRecorder() schemaInfo.ServeHTTP(response, request) request, _ = http.NewRequest("GET", "/debug/unknown", nil) response = httptest.NewRecorder() schemaInfo.ServeHTTP(response, request) }
func TestCachePoolMemcacheStatsFail(t *testing.T) { cache := fakecacheservice.Register() fakesqldb.Register() rowCacheConfig := RowCacheConfig{ Binary: "ls", Connections: 100, } cachePool := newTestCachePool(rowCacheConfig, true) idleTimeout := 1 * time.Second cachePool.idleTimeout = idleTimeout cachePool.Open() defer cachePool.Close() memcacheStatsBefore := cachePool.queryServiceStats.InternalErrors.Counts()["MemcacheStats"] // any memcache calls should fail cache.EnableCacheServiceError() cachePool.memcacheStats.update() memcacheStatsAfter := cachePool.queryServiceStats.InternalErrors.Counts()["MemcacheStats"] if memcacheStatsAfter <= memcacheStatsBefore { t.Fatalf("memcache stats should cause an internal error") } }
func TestTableInfoNew(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getTestTableInfoQueries() { db.AddQuery(query, result) } cachePool := newTestTableInfoCachePool() cachePool.Open() defer cachePool.Close() tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db) if err != nil { t.Fatalf("failed to create a test table info") } if tableInfo.Cache == nil { t.Fatalf("rowcache should be enabled") } stats := tableInfo.StatsJSON() if stats == "" || stats == "null" { t.Fatalf("rowcache is enabled, stats should not be empty or null") } }
func TestSchemaInfoOpenFailedDueToInvalidTimeFormat(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() db.AddQuery("select unix_timestamp()", &mproto.QueryResult{ RowsAffected: 1, Rows: [][]sqltypes.Value{ // make safety check fail, invalid time format []sqltypes.Value{sqltypes.MakeString([]byte("invalid_time"))}, }, }) schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false) appParams := sqldb.ConnParams{Engine: db.Name} dbaParams := sqldb.ConnParams{Engine: db.Name} schemaInfo.cachePool.Open() defer schemaInfo.cachePool.Close() defer handleAndVerifyTabletError( t, "schema info Open should fail because it could not get MySQL time", ErrFail, ) schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, false) }
func TestSchemaInfoQueryCache(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoTestSupportedQueries() { db.AddQuery(query, result) } firstQuery := "select * from test_table_01" secondQuery := "select * from test_table_02" db.AddQuery("select * from test_table_01 where 1 != 1", &mproto.QueryResult{}) db.AddQuery("select * from test_table_02 where 1 != 1", &mproto.QueryResult{}) schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, true) appParams := sqldb.ConnParams{} dbaParams := sqldb.ConnParams{} cachePool := newTestSchemaInfoCachePool(true, schemaInfo.queryServiceStats) cachePool.Open() defer cachePool.Close() schemaOverrides := getSchemaInfoTestSchemaOverride() // test cache type RW schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, cachePool, true) defer schemaInfo.Close() ctx := context.Background() logStats := newLogStats("GetPlanStats", ctx) schemaInfo.SetQueryCacheSize(1) firstPlan := schemaInfo.GetPlan(ctx, logStats, firstQuery) if firstPlan == nil { t.Fatalf("plan should not be nil") } secondPlan := schemaInfo.GetPlan(ctx, logStats, secondQuery) if secondPlan == nil { t.Fatalf("plan should not be nil") } expvar.Do(func(kv expvar.KeyValue) { _ = kv.Value.String() }) schemaInfo.ClearQueryPlanCache() }
func TestSchemaInfoQueryCacheFailDueToInvalidCacheSize(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() for query, result := range getSchemaInfoTestSupportedQueries() { db.AddQuery(query, result) } schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, false) appParams := sqldb.ConnParams{Engine: db.Name} dbaParams := sqldb.ConnParams{Engine: db.Name} schemaInfo.cachePool.Open() defer schemaInfo.cachePool.Close() schemaOverrides := getSchemaInfoTestSchemaOverride() // test cache type RW schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, true) defer schemaInfo.Close() defer handleAndVerifyTabletError( t, "schema info SetQueryCacheSize should use a positive size", ErrFail, ) schemaInfo.SetQueryCacheCap(0) }
func TestSchemaInfoOpenFailedDueToMissMySQLTime(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() db.AddQuery("select unix_timestamp()", &mproto.QueryResult{ // make this query fail RowsAffected: math.MaxUint64, Rows: [][]sqltypes.Value{ []sqltypes.Value{sqltypes.MakeString([]byte("1427325875"))}, }, }) schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false) appParams := sqldb.ConnParams{Engine: db.Name} dbaParams := sqldb.ConnParams{Engine: db.Name} schemaInfo.cachePool.Open() defer schemaInfo.cachePool.Close() defer handleAndVerifyTabletError( t, "schema info Open should fail because of it could not get MySQL time", ErrFail, ) schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, false) }
func TestSchemaInfoOpenFailedDueToIncorrectMysqlRowNum(t *testing.T) { fakecacheservice.Register() db := fakesqldb.Register() db.AddQuery("select unix_timestamp()", &mproto.QueryResult{ RowsAffected: 1, Rows: [][]sqltypes.Value{ // make this query fail nil, }, }) schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false) appParams := sqldb.ConnParams{Engine: db.Name} dbaParams := sqldb.ConnParams{Engine: db.Name} schemaInfo.cachePool.Open() defer schemaInfo.cachePool.Close() defer handleAndVerifyTabletError( t, "schema info Open should fail because of incorrect MySQL row number", ErrFail, ) schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, false) }