laskoviymishka commented on code in PR #2376: URL: https://github.com/apache/iceberg-go/pull/2376#discussion_r4580372016
########## catalog/rest/resttest/resttest.go: ########## @@ -0,0 +1,378 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package resttest + +import ( + "encoding/json" + "errors" + "fmt" + "maps" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "sync" + "testing" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/table" +) + +// maxRequestBytes bounds request bodies the server decodes. +const maxRequestBytes = 16 << 20 + +// Option configures a handler built by NewHandler or NewServer. +type Option func(*config) + +type config struct { + warehouse string + prefix string + defaults iceberg.Properties + overrides iceberg.Properties + errs []error +} + +// WithWarehouse sets the warehouse location, such as "file://"+t.TempDir(). +// A table created without an explicit location, in a namespace without a +// "location" property, is placed under <warehouse>/<namespace>.db/<table>. +// Without a warehouse such a create fails with 400. +func WithWarehouse(uri string) Option { + return func(c *config) { c.warehouse = uri } +} + +// WithPrefix serves the catalog under /v1/{prefix}/ and returns the prefix in +// the config response overrides, so a client picks it up without being told. +func WithPrefix(prefix string) Option { + return func(c *config) { c.prefix = strings.Trim(prefix, "/") } +} + +// WithProperties sets the defaults and overrides the config endpoint returns. +// The server always sets the "prefix" override itself when WithPrefix is used. +// +// The "prefix" and "namespace-separator" keys change the paths the client +// sends, which the server does not follow, so NewHandler panics if either +// appears. Use WithPrefix to serve under a prefix. +func WithProperties(defaults, overrides iceberg.Properties) Option { + return func(c *config) { + for _, props := range []iceberg.Properties{defaults, overrides} { + for _, key := range unsupportedConfigKeys { + if _, ok := props[key]; ok { + c.errs = append(c.errs, fmt.Errorf("resttest: WithProperties does not support %q", key)) + } + } + } + c.defaults = maps.Clone(defaults) + c.overrides = maps.Clone(overrides) + } +} + +// unsupportedConfigKeys are config properties that change client routing in +// ways the server does not follow. +var unsupportedConfigKeys = []string{"prefix", "namespace-separator"} + +// NewHandler returns an in-memory REST catalog. It is safe for concurrent use. +// It panics if an option is invalid. +func NewHandler(opts ...Option) http.Handler { + s := &server{ + namespaces: map[string]iceberg.Properties{}, + tables: map[string]*tableEntry{}, + } + for _, opt := range opts { + opt(&s.cfg) + } + if err := errors.Join(s.cfg.errs...); err != nil { + panic(err) + } + if s.cfg.prefix != "" { + s.prefixSegments = strings.Split(s.cfg.prefix, "/") + } + + return s +} + +// NewServer starts NewHandler on an httptest server and closes it when the +// test ends. Unless WithWarehouse is given, the warehouse is a temporary +// directory owned by t. +func NewServer(t testing.TB, opts ...Option) *httptest.Server { + t.Helper() + + opts = append([]Option{WithWarehouse("file://" + t.TempDir())}, opts...) + srv := httptest.NewServer(NewHandler(opts...)) + t.Cleanup(srv.Close) + + return srv +} + +type tableEntry struct { + namespace []string + name string + metadataLoc string + metadata table.Metadata +} + +type server struct { + cfg config + prefixSegments []string + + // mu guards namespaces and tables. Handlers hold it for the whole + // request, including metadata file writes, which keeps commits atomic. + mu sync.Mutex + // namespaces maps a namespace key (levels joined by nsSep) to its + // properties. + namespaces map[string]iceberg.Properties + // tables maps tableKey(namespace, name) to the current table pointer. + tables map[string]*tableEntry +} + +// nsSep joins namespace levels. It is the decoded form of the REST client's +// default "%1F" separator. +const nsSep = "\x1f" + +func nsKey(ns []string) string { return strings.Join(ns, nsSep) } + +func tableKey(ns []string, name string) string { return nsKey(ns) + "\x00" + name } + +func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { + segments, err := pathSegments(r.URL.EscapedPath()) + if err != nil { + writeError(w, r, http.StatusBadRequest, typeBadRequest, err.Error()) + + return + } + if len(segments) == 0 || segments[0] != "v1" { + writeError(w, r, http.StatusNotFound, typeNotFound, "unknown path "+r.URL.Path) + + return + } + segments = segments[1:] + + if len(segments) == 1 && segments[0] == "config" { + if r.Method != http.MethodGet { + writeError(w, r, http.StatusMethodNotAllowed, typeBadRequest, "config only supports GET") + + return + } + s.handleConfig(w) + + return + } + + if len(segments) < len(s.prefixSegments) { + writeError(w, r, http.StatusNotFound, typeNotFound, "unknown path "+r.URL.Path) + + return + } + for i, p := range s.prefixSegments { + if segments[i] != p { + writeError(w, r, http.StatusNotFound, typeNotFound, "unknown prefix in "+r.URL.Path) + + return + } + } + segments = segments[len(s.prefixSegments):] + + if r.Body != nil { + r.Body = http.MaxBytesReader(w, r.Body, maxRequestBytes) + } + + s.mu.Lock() Review Comment: Could we read the body before taking `s.mu`? Every handler streams `r.Body` through [`decodeJSON`](https://github.com/apache/iceberg-go/blob/435736ca5c45be2483fe04299a638813af9c5d0e/catalog/rest/resttest/resttest.go#L333-L342) inside the lock, so one slow upload stalls all other requests. Nothing hits that today, but slow or fault-injecting client transports would. Buffering here, capped by `MaxBytesReader`, keeps commits atomic since the lock still covers validation and the metadata write. ########## catalog/rest/resttest/resttest.go: ########## @@ -0,0 +1,378 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package resttest + +import ( + "encoding/json" + "errors" + "fmt" + "maps" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "sync" + "testing" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/table" +) + +// maxRequestBytes bounds request bodies the server decodes. +const maxRequestBytes = 16 << 20 + +// Option configures a handler built by NewHandler or NewServer. +type Option func(*config) + +type config struct { + warehouse string + prefix string + defaults iceberg.Properties + overrides iceberg.Properties + errs []error +} + +// WithWarehouse sets the warehouse location, such as "file://"+t.TempDir(). +// A table created without an explicit location, in a namespace without a +// "location" property, is placed under <warehouse>/<namespace>.db/<table>. +// Without a warehouse such a create fails with 400. +func WithWarehouse(uri string) Option { + return func(c *config) { c.warehouse = uri } +} + +// WithPrefix serves the catalog under /v1/{prefix}/ and returns the prefix in +// the config response overrides, so a client picks it up without being told. +func WithPrefix(prefix string) Option { + return func(c *config) { c.prefix = strings.Trim(prefix, "/") } +} + +// WithProperties sets the defaults and overrides the config endpoint returns. +// The server always sets the "prefix" override itself when WithPrefix is used. +// +// The "prefix" and "namespace-separator" keys change the paths the client +// sends, which the server does not follow, so NewHandler panics if either +// appears. Use WithPrefix to serve under a prefix. +func WithProperties(defaults, overrides iceberg.Properties) Option { + return func(c *config) { + for _, props := range []iceberg.Properties{defaults, overrides} { + for _, key := range unsupportedConfigKeys { + if _, ok := props[key]; ok { + c.errs = append(c.errs, fmt.Errorf("resttest: WithProperties does not support %q", key)) + } + } + } + c.defaults = maps.Clone(defaults) + c.overrides = maps.Clone(overrides) + } +} + +// unsupportedConfigKeys are config properties that change client routing in +// ways the server does not follow. +var unsupportedConfigKeys = []string{"prefix", "namespace-separator"} + +// NewHandler returns an in-memory REST catalog. It is safe for concurrent use. +// It panics if an option is invalid. +func NewHandler(opts ...Option) http.Handler { + s := &server{ + namespaces: map[string]iceberg.Properties{}, + tables: map[string]*tableEntry{}, + } + for _, opt := range opts { + opt(&s.cfg) + } + if err := errors.Join(s.cfg.errs...); err != nil { + panic(err) + } + if s.cfg.prefix != "" { + s.prefixSegments = strings.Split(s.cfg.prefix, "/") + } + + return s +} + +// NewServer starts NewHandler on an httptest server and closes it when the +// test ends. Unless WithWarehouse is given, the warehouse is a temporary +// directory owned by t. +func NewServer(t testing.TB, opts ...Option) *httptest.Server { + t.Helper() + + opts = append([]Option{WithWarehouse("file://" + t.TempDir())}, opts...) + srv := httptest.NewServer(NewHandler(opts...)) Review Comment: Could `NewServer` fail through `t` instead of panicking? It already has a `testing.TB`, and a panic stops the whole package binary, so one bad option hides every later test. In a stdlib-only repro, a panicking test prints `panic: setup [recovered, repanicked]` and the next test never starts. An unexported constructor returning the error would let `NewHandler` keep its documented panic while `NewServer` calls `t.Fatal`. ########## catalog/rest/resttest/resttest_test.go: ########## @@ -0,0 +1,452 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package resttest_test + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/catalog" + "github.com/apache/iceberg-go/catalog/catalogtest" + "github.com/apache/iceberg-go/catalog/rest" + "github.com/apache/iceberg-go/catalog/rest/resttest" + "github.com/apache/iceberg-go/table" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func newCatalog(t *testing.T, url string) *rest.Catalog { + t.Helper() + + cat, err := rest.NewCatalog(context.Background(), "resttest", url) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, cat.Close()) }) + + return cat +} + +func TestConformance(t *testing.T) { + catalogtest.RunCatalogTests(t, catalogtest.Config{ + SupportsNamespaceProperties: true, + SupportsRenameTable: true, + NewCatalog: func(t *testing.T) catalog.Catalog { + return newCatalog(t, resttest.NewServer(t).URL) + }, + }) +} + +// The prefix is only returned in the config response, so this also checks +// the client picks it up from there. +func TestConformanceWithPrefix(t *testing.T) { + catalogtest.RunCatalogTests(t, catalogtest.Config{ + SupportsNamespaceProperties: true, + SupportsRenameTable: true, + NewCatalog: func(t *testing.T) catalog.Catalog { + return newCatalog(t, resttest.NewServer(t, resttest.WithPrefix("warehouses/wh1")).URL) + }, + }) +} + +func createTable(t *testing.T, cat catalog.Catalog, props iceberg.Properties) (table.Identifier, *table.Table) { + t.Helper() + ctx := context.Background() + + ns := table.Identifier{"db"} + if exists, err := cat.CheckNamespaceExists(ctx, ns); err == nil && !exists { + require.NoError(t, cat.CreateNamespace(ctx, ns, nil)) + } + ident := table.Identifier{"db", fmt.Sprintf("tbl_%d", tableSeq.Add(1))} + tbl, err := cat.CreateTable(ctx, ident, catalogtest.Schema, catalog.WithProperties(props)) + require.NoError(t, err) + + return ident, tbl +} + +var tableSeq atomic.Int64 + +func addColumn(t *testing.T, tbl *table.Table, name string) *table.Transaction { + t.Helper() + + txn := tbl.NewTransaction() + require.NoError(t, txn.UpdateSchema(true, false). + AddColumn([]string{name}, iceberg.PrimitiveTypes.String, "", false, nil). + Commit()) + + return txn +} + +// Two writers start from the same metadata; the second commit's schema +// requirement no longer holds and fails as a conflict, and the table keeps +// the first writer's change. +func TestConcurrentSchemaCommitsConflict(t *testing.T) { + ctx := context.Background() + cat := newCatalog(t, resttest.NewServer(t).URL) + ident, first := createTable(t, cat, iceberg.Properties{table.CommitNumRetriesKey: "0"}) + + second, err := cat.LoadTable(ctx, ident) + require.NoError(t, err) + + _, err = addColumn(t, first, "winner").Commit(ctx) + require.NoError(t, err) + + _, err = addColumn(t, second, "loser").Commit(ctx) + require.ErrorIs(t, err, table.ErrCommitFailed) + + current, err := cat.LoadTable(ctx, ident) + require.NoError(t, err) + _, ok := current.Schema().FindFieldByName("winner") + assert.True(t, ok) + _, ok = current.Schema().FindFieldByName("loser") + assert.False(t, ok) +} + +// Property updates carry no requirement that a concurrent writer breaks, so +// many writers committing at once must all land without losing an update. +func TestConcurrentPropertyCommits(t *testing.T) { + ctx := context.Background() + cat := newCatalog(t, resttest.NewServer(t).URL) + ident, _ := createTable(t, cat, nil) + + const writers = 8 + var wg sync.WaitGroup + errs := make(chan error, writers) + for i := range writers { + wg.Go(func() { + tbl, err := cat.LoadTable(ctx, ident) + if err != nil { + errs <- err + + return + } + txn := tbl.NewTransaction() + if err := txn.SetProperties(iceberg.Properties{fmt.Sprintf("writer-%d", i): "1"}); err != nil { + errs <- err + + return + } + _, err = txn.Commit(ctx) + errs <- err + }) + } + wg.Wait() + close(errs) + for err := range errs { + require.NoError(t, err) + } + + tbl, err := cat.LoadTable(ctx, ident) + require.NoError(t, err) + for i := range writers { + assert.Equal(t, "1", tbl.Properties()[fmt.Sprintf("writer-%d", i)]) + } +} + +func TestExistsChecksUseHead(t *testing.T) { + var heads atomic.Int64 + h := resttest.NewHandler(resttest.WithWarehouse("file://" + t.TempDir())) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodHead { + heads.Add(1) + } + h.ServeHTTP(w, r) + })) + t.Cleanup(srv.Close) + + ctx := context.Background() + cat := newCatalog(t, srv.URL) + ident, _ := createTable(t, cat, nil) + + exists, err := cat.CheckNamespaceExists(ctx, table.Identifier{"missing"}) + require.NoError(t, err) + assert.False(t, exists) + + exists, err = cat.CheckTableExists(ctx, ident) + require.NoError(t, err) + assert.True(t, exists) + + exists, err = cat.CheckTableExists(ctx, table.Identifier{"db", "missing"}) + require.NoError(t, err) + assert.False(t, exists) + + assert.GreaterOrEqual(t, heads.Load(), int64(3)) +} + +func TestRegisterTable(t *testing.T) { + ctx := context.Background() + cat := newCatalog(t, resttest.NewServer(t).URL) + ident, tbl := createTable(t, cat, nil) + + registered := table.Identifier{"db", "registered"} + got, err := cat.RegisterTable(ctx, registered, tbl.MetadataLocation()) + require.NoError(t, err) + assert.Equal(t, tbl.Metadata().TableUUID(), got.Metadata().TableUUID()) + assert.True(t, got.Schema().Equals(catalogtest.TableSchema)) + + _, err = cat.RegisterTable(ctx, ident, tbl.MetadataLocation()) + require.ErrorIs(t, err, catalog.ErrTableAlreadyExists) + + _, err = cat.RegisterTable(ctx, table.Identifier{"missing", "t"}, tbl.MetadataLocation()) + require.ErrorIs(t, err, catalog.ErrNoSuchNamespace) +} + +// The server's own compressed metadata files must be registrable. +func TestRegisterCompressedMetadata(t *testing.T) { + ctx := context.Background() + cat := newCatalog(t, resttest.NewServer(t).URL) + _, tbl := createTable(t, cat, iceberg.Properties{ + table.MetadataCompressionKey: table.MetadataCompressionCodecGzip, + }) + require.True(t, strings.HasSuffix(tbl.MetadataLocation(), ".gz.metadata.json"), tbl.MetadataLocation()) + + got, err := cat.RegisterTable(ctx, table.Identifier{"db", "registered_gzip"}, tbl.MetadataLocation()) + require.NoError(t, err) + assert.Equal(t, tbl.Metadata().TableUUID(), got.Metadata().TableUUID()) +} + +// catalogtest does not cover nested namespaces yet, so check listing by +// parent and that a namespace with children cannot be dropped. +func TestNestedNamespaces(t *testing.T) { + ctx := context.Background() + cat := newCatalog(t, resttest.NewServer(t).URL) + + require.NoError(t, cat.CreateNamespace(ctx, table.Identifier{"a"}, nil)) + require.NoError(t, cat.CreateNamespace(ctx, table.Identifier{"a", "b"}, nil)) + require.NoError(t, cat.CreateNamespace(ctx, table.Identifier{"a", "c"}, nil)) + require.NoError(t, cat.CreateNamespace(ctx, table.Identifier{"a", "b", "d"}, nil)) + + err := cat.CreateNamespace(ctx, table.Identifier{"missing", "child"}, nil) + require.ErrorIs(t, err, catalog.ErrNoSuchNamespace) + + top, err := cat.ListNamespaces(ctx, nil) + require.NoError(t, err) + assert.Equal(t, []table.Identifier{{"a"}}, top) + + children, err := cat.ListNamespaces(ctx, table.Identifier{"a"}) + require.NoError(t, err) + assert.Equal(t, []table.Identifier{{"a", "b"}, {"a", "c"}}, children) + + _, err = cat.ListNamespaces(ctx, table.Identifier{"missing"}) + require.ErrorIs(t, err, catalog.ErrNoSuchNamespace) + + require.ErrorIs(t, cat.DropNamespace(ctx, table.Identifier{"a"}), catalog.ErrNamespaceNotEmpty) + require.ErrorIs(t, cat.DropNamespace(ctx, table.Identifier{"a", "b"}), catalog.ErrNamespaceNotEmpty) + + require.NoError(t, cat.DropNamespace(ctx, table.Identifier{"a", "b", "d"})) + require.NoError(t, cat.DropNamespace(ctx, table.Identifier{"a", "b"})) + require.NoError(t, cat.DropNamespace(ctx, table.Identifier{"a", "c"})) + require.NoError(t, cat.DropNamespace(ctx, table.Identifier{"a"})) +} + +// A create with staged updates sends stage-create, which registers nothing, +// and then finishes with an assert-create commit. +func TestStagedCreate(t *testing.T) { + var commits atomic.Int64 + h := resttest.NewHandler(resttest.WithWarehouse("file://" + t.TempDir())) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/tables/staged") { + commits.Add(1) + } + h.ServeHTTP(w, r) + })) + t.Cleanup(srv.Close) + + ctx := context.Background() + cat := newCatalog(t, srv.URL) + require.NoError(t, cat.CreateNamespace(ctx, table.Identifier{"db"}, nil)) + + ident := table.Identifier{"db", "staged"} + tbl, err := cat.CreateTable(ctx, ident, catalogtest.Schema, + catalog.WithStagedUpdates(table.NewSetPropertiesUpdate(iceberg.Properties{"staged": "yes"}))) + require.NoError(t, err) + assert.Equal(t, int64(1), commits.Load(), "the create must finish with one commit") + assert.Equal(t, "yes", tbl.Properties()["staged"]) + + loaded, err := cat.LoadTable(ctx, ident) + require.NoError(t, err) + assert.Equal(t, "yes", loaded.Properties()["staged"]) + assert.Equal(t, tbl.MetadataLocation(), loaded.MetadataLocation()) + + // A second staged create of the same table is rejected by assert-create. Review Comment: +1 to @slachiewicz's note: this 409 comes from the existence check in [`createTable`](https://github.com/apache/iceberg-go/blob/435736ca5c45be2483fe04299a638813af9c5d0e/catalog/rest/resttest/tables.go#L107-L111), before the `StageCreate` branch. Covering the `assert-create` conflict doesn't need stage-then-create. A raw `TestSpecErrorResponses` row that POSTs `{"requirements":[{"type":"assert-create"}],"updates":[]}` to the seeded `db.t` reaches [this 409](https://github.com/apache/iceberg-go/blob/435736ca5c45be2483fe04299a638813af9c5d0e/catalog/rest/resttest/tables.go#L319-L325). ```suggestion // A second staged create of the same table is refused at stage-create: // createTable returns 409 for an existing table before the StageCreate branch. ``` ########## catalog/rest/resttest/resttest_test.go: ########## @@ -0,0 +1,452 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package resttest_test + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/catalog" + "github.com/apache/iceberg-go/catalog/catalogtest" + "github.com/apache/iceberg-go/catalog/rest" + "github.com/apache/iceberg-go/catalog/rest/resttest" + "github.com/apache/iceberg-go/table" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func newCatalog(t *testing.T, url string) *rest.Catalog { + t.Helper() + + cat, err := rest.NewCatalog(context.Background(), "resttest", url) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, cat.Close()) }) + + return cat +} + +func TestConformance(t *testing.T) { + catalogtest.RunCatalogTests(t, catalogtest.Config{ + SupportsNamespaceProperties: true, + SupportsRenameTable: true, + NewCatalog: func(t *testing.T) catalog.Catalog { + return newCatalog(t, resttest.NewServer(t).URL) + }, + }) +} + +// The prefix is only returned in the config response, so this also checks +// the client picks it up from there. +func TestConformanceWithPrefix(t *testing.T) { + catalogtest.RunCatalogTests(t, catalogtest.Config{ + SupportsNamespaceProperties: true, + SupportsRenameTable: true, + NewCatalog: func(t *testing.T) catalog.Catalog { + return newCatalog(t, resttest.NewServer(t, resttest.WithPrefix("warehouses/wh1")).URL) + }, + }) +} + +func createTable(t *testing.T, cat catalog.Catalog, props iceberg.Properties) (table.Identifier, *table.Table) { + t.Helper() + ctx := context.Background() + + ns := table.Identifier{"db"} + if exists, err := cat.CheckNamespaceExists(ctx, ns); err == nil && !exists { + require.NoError(t, cat.CreateNamespace(ctx, ns, nil)) + } + ident := table.Identifier{"db", fmt.Sprintf("tbl_%d", tableSeq.Add(1))} + tbl, err := cat.CreateTable(ctx, ident, catalogtest.Schema, catalog.WithProperties(props)) + require.NoError(t, err) + + return ident, tbl +} + +var tableSeq atomic.Int64 + +func addColumn(t *testing.T, tbl *table.Table, name string) *table.Transaction { + t.Helper() + + txn := tbl.NewTransaction() + require.NoError(t, txn.UpdateSchema(true, false). + AddColumn([]string{name}, iceberg.PrimitiveTypes.String, "", false, nil). + Commit()) + + return txn +} + +// Two writers start from the same metadata; the second commit's schema +// requirement no longer holds and fails as a conflict, and the table keeps +// the first writer's change. +func TestConcurrentSchemaCommitsConflict(t *testing.T) { Review Comment: Could this get a concurrent variant: N goroutines committing schema changes from the same loaded table, asserting exactly one succeeds and the rest get `table.ErrCommitFailed`? Here the two commits run one after the other, and the only concurrent test, `TestConcurrentPropertyCommits`, uses non-conflicting property writers ([L101-L165](https://github.com/apache/iceberg-go/blob/435736ca5c45be2483fe04299a638813af9c5d0e/catalog/rest/resttest/resttest_test.go#L101-L165)). So the commit lock is never tested against a real conflict. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
