Skip to content

Test against over spending #2024

Answered by ostafen
pierrekttipay asked this question in Q&A
Sep 15, 2024 · 5 comments · 8 replies
Discussion options

You must be logged in to vote

The following snippets creates an accounts table with two simple fields: id and balance. Then one account is inserted, with an initial balance of 65. Note the CHECK balance >= 0 part, which makes sure no update can result in a negative balance.

The snippet then spawns 10 concurrent goroutines, each trying to decrement the balance by 10 (they try as soon as a read conflict happens). If a unique constraint violation is detected the goroutine simply stops, as it would otherwise loop forever.

If all works, we expect the balance to be decremented at most 6 times, with a final balance of value of 5.

package main

import (
	"context"
	"errors"
	"fmt"
	"log"
	"sync"

	"github.com/codenotary/immud…

Replies: 5 comments 8 replies

Comment options

You must be logged in to vote
3 replies
@pierrekttipay
Comment options

@ostafen
Comment options

Answer selected by pierrekttipay
@pierrekttipay
Comment options

Comment options

You must be logged in to vote
5 replies
@ostafen
Comment options

@pierrekttipay
Comment options

@pierrekttipay
Comment options

@ostafen
Comment options

@pierrekttipay
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants